Keyboard isn't responsive after unlock OS-X (Mavericks) using AppleScript - objective-c

I'm using AppleScript to lock the Mac from an external device (trigger) and unlock it from this same device.
Locking works great but when unlock my Mac keyboard and pads becomes unresponsive. Weird thing is when I move mousse after locking and get the password login if I enter the password the unlock makes keyboard responsive and only then.
Is it some kind of security settings I'm not aware of? How could I solve it?
Here is the script I'm using:
Lock:
tell application "System Events"
tell security preferences
set require password to wake to true
end tell
end tell
activate application "ScreenSaverEngine"
Unlock:
tell application "System Events"
tell security preferences
set require password to wake to false
end tell
end tell
tell application "ScreenSaverEngine" to quit
Please help I'm desperate. Also if you have another method you know to lock/unlock my Mac using code I would love to try it out!
Thanks!
UPDATE: To be clear when removing the password setting code (require password) the issue doesn't appear.
Meaning removing those 3 lines:
tell security preferences
set require password to wake to true
end tell
Doesn't have this issue, this is why I think it could be some security thing I'm not aware of.
UPDATE (26 Dec 2013): I didn't find any solution and bounty finished so what I'm doing is inserting my password using ActionScript (maybe this could help others having same issue). If you do have another solution I would love to know.

Try below for locking the screen, I am using the same and no issues while locking or unlocking. Hope this helps.
try
tell application "System Events"
set the process_flag to (exists process "ScreenSaverEngine")
end tell
if the process_flag is true then
ignoring application responses
tell application "System Events" to quit
end ignoring
else
set the target_app to ((path to "dlib" from system domain as string) & "Frameworks:ScreenSaver.framework:Versions:A:Resources:ScreenSaverEngine.app") as alias
tell application (target_app as string) to launch
end if
end try

Later today I'll be able to do more testing on a Mavericks machine. In the meantime, I have a little theory -- have you tested this with other screen savers "active"? I say this because I do have a couple of screen savers that are a little buggy, and I know that at least one of them needs an update for Mavericks. Which one are you using?
[UPDATE:] I did get on Mavericks and did not see the problems you described, or at least problems of that severity. I did get a little weirdness, though, possibly having to do with timing. What if you structure your script like this? For me, it covers for errors like "connection invalid" (I admit this is a little 'dangerous'; it might be good to put an upper limit of how many times it 'try's the activate by putting a counter in there):
tell application "System Events"
tell security preferences
set require password to wake to false
end tell
end tell
set connectionValid to false
repeat until connectionValid
try
activate application "ScreenSaverEngine"
set connectionValid to true
on error e
end try
end repeat

Related

Create a new PSD file and copy/paste new layer with AppleScript

I try to make a code assembly to create my own applescript to create an image in Photoshop but unfortunately I have an error and I can't find where it comes from.
The first part of the script works, the random image is well copied to the clipboard but the rest is problematic.
-- Select random image
tell application "Finder"
set bg_img to file (random number from 1 to (count files of folder "HD:Works:Club:LAYERS:0-BG")) of folder "HD:Works:Club:LAYERS:0-BG"
end tell
tell application "Preview"
activate
open bg_img
end tell
-- Select and copy image to clipboard
tell application "System Events"
tell process "Preview"
keystroke "a" using command down
keystroke "c" using command down
end tell
end tell
-- Quit Preview Application
tell application "Preview" to quit
tell application "Adobe Photoshop 2022"
-- Create a new document
set docRef to make new document with properties {width:1200, height:1200, resolution:72}
tell docRef
-- Unlock the background layer and fill it with gray color
set background layer of layer 1 of docRef to false
fill selection with contents {class:RGB color, red:200, green:200, blue:200}
end tell
end tell
-- Paste image to new layer
tell application "System Events"
tell process "Adobe Photoshop 2022"
set newLayer to make art layer with properties {name:"LayerA"}
keystroke "v" using command down
end tell
end tell
Ok, a compilation error; I got it now. Try this:
-- Paste image to new layer
tell application "Adobe Photoshop 2022"
activate
set newLayer to make art layer with properties {name:"LayerA"}
end tell
tell application "System Events"
tell process "Adobe Photoshop 2022"
keystroke "v" using command down
end tell
end tell
Remember, you use a tell application "NAME" … end tell block to 1. tell AppleScript which app to send commands to, and 2. the app from which to get the custom terminology (keywords) for commands and objects.
Thus your tell application "System Events"…end tell block uses SE terminology and commands. The tell process "Photoshop"…end tell block inside it is still addressed to System Events. process is an SE-defined keyword for an SE-specific object. Likewise keystroke is an SE-specific command.
To make a new art layer in Photoshop, you have to put that command inside its own tell application block which is targeted at PS.
Yeah, AppleScript is often confusing. And System Events is 10× more confusing than that, thanks to it being not-obvious what is GUI Scripting and what isn’t. Good luck.
--
p.s. I’ve a feeling the tell process block is totally redundant, and is only necessary when using GUI Scripting to manipulate an app’s windows. IIRC the keystroke command doesn’t know or care what app receives those keys, in which case what’s important is that you activate Photoshop before you start “pressing” keys. I’ll leave it to you to determine if you can discard the tell process.
p.p.s. AppleScriptable apps usually don’t include cut and paste commands, usually ’cos they already provide far better ways of getting and setting content. Photoshop, however, does. So if you’ve not already tried it, try inserting a paste command at the end of your “tell Photoshop” block, then comment out the “tell System Events” block, and see if that works for you. If so, you can get rid of that System Events block entirely!
p.p.p.s. It’s also worth investigating PS’s dictionary further, to see if it can open and place your “bg_file” directly. If so, you can eliminate the “tell Preview” and the rest of that System Events horridness too!!

Using applescript to open a page in notion (on Mac)

I am trying to set up a script to open a specific page in the notion app. So far I've tried the following:
tell application "Safari"
activate
tell window 1
set current tab to (make new tab with properties {URL:"notion://www.notion.so/Focus-Mode-5eca2183453748d3b5acaa8e7292ef83"})
end tell
end tell
but Safari requires that you give permission to open the app. Is there anyway to get around this? If not, is there anyway to open the page in Notion without having to go through Safari?
It can be even simpler:
I was playing with your solution and found out that even though you can't finde those commands in the dictionary, they still exist:
tell application "Notion"
open location "notion:page:Focus-Mode-5eca2183453748d3b5acaa8e7292ef83"
end tell
I figured out a solution - this was painfully obvious...
tell application "Notion"
open location "notion://www.notion.so/Focus-Mode-5eca2183453748d3b5acaa8e7292ef83"
end tell

Turn Bluetooth off and on programmatically

Is there a way to programmatically turn on and off the Bluetooth connection on OSX in a way that will be accepted by the Mac App Store?
From my previous question, I've found about blueutil, but it uses private APIs.
It would be somewhat surprising if Apple approved an app that modified the user's antenna settings. It sounds like the kind of thing they typically frown on, no matter how you do it. But then, sometimes I get surprised.
You can definitely do it through Applescript:
tell application "System Preferences"
set current pane to pane "com.apple.preferences.Bluetooth"
tell application "System Events"
tell process "System Preferences"
set isOnCheckbox to checkbox "On" of window "Bluetooth"
if value of isOnCheckbox is 0 then
click isOnCheckbox
end if
end tell
end tell
quit
end tell
Note that this will take over System Preferences and at the end close it even if the user was running it. That's not the best user experience, and I definitely wouldn't do it without first warning the user. But of course, I wouldn't recommend modifying the bluetooth settings without warning the user.
EDIT
Because you asked, I'll take a moment to rant here....
Regarding how to learn to read and write the above, first note that it, like most AppleScript I write professionally, was cobbled together from google searches and experimentation. I'm a purist programmer at heart, and I believe in really understanding the technology you use. Even I cobble together things in AppleScript until they "kind of work."
I wish there were a really good document. Of course there's the language guide, but it's kind of like learning Cocoa from the ObjC language definition. My current recommendations are Beginning AppleScript and then AppleScript: The Definitive Guide. Neuburg in particular does not sugarcoat the language or pretend that it makes sense. Applescript, even worse than the original COBOL (ADD X TO Y GIVING Z), is very hard to write because it tries so hard to be easy. I love and respect many languages. AppleScript is crap.
It is, however, the most supported way to interact with most of the Mac system functions, so a good Mac developer needs to at least be able to get by in it. Even if you use the new ScriptingBridge via ObjC (or MacRuby), the underlying object model is still AppleScript based. In my experience, to get ScriptingBridge code to work well, you generally have to write it first in AppleScript and then translate it into Cocoa.
This worked for me in 10.15.6, I might have over complicated my solution which is running script 1 (turn bluetooth off) and then script 2 (turn bluetooth on).
Script 1. This is for turning bluetooth OFF
tell application "System Events" to tell process "SystemUIServer"
tell (menu bar item 1 of menu bar 1 where description is "bluetooth")
click
click menu item "Turn Bluetooth Off" of menu 1
end tell
tell window 1
click button "Turn Bluetooth Off"
end tell
end tell
Script 2. This is for turning bluetooth ON
tell application "System Events" to tell process "SystemUIServer"
tell (menu bar item 1 of menu bar 1 where description is "bluetooth")
click
click menu item "Turn Bluetooth On" of menu 1
end tell
end tell
So I execute one command which will run one script after the other, the sleep is for the UI to update properly.
osascript bluetooth_off.scpt && sleep 3s && osascript bluetooth_on.scpt
You can just save the command in a file and execute it using: (they have to be in the same directory).
~ bash <fileName>
Note: You will need to give access to the terminal on Preferences > Security & Privacy

Change Dialog Box Text With AppleScript?

Simply, is it possible to change a "static text" field in an Apple system dialog box? Specifically I am looking into changing some label text of a password popup. I have tried using AppleScript via GUI scripting:
tell application "System Events"
try
tell window 1 of process "loginwindow"
repeat until not (value of static text 4 is equal to "")
set value of static text 4 to "this is a test"
delay 0.5
end repeat
end tell
end try
end tell
Although this does not seem to work at all. Any other methods? Perhaps Scripting Bridge or other? I'd prefer not to code my own dialogs from scratch (not for difficulty reasons, I could easily do it, I'd just prefer to interface rather than emulate.)
Thanks!
Here's an idea... I don't think you can change the login window text itself but you can change the background picture of the login window. Therefore you may be able to setup some images, and then use those images during the login process to display what you want.
I never tried it but this post shows how to do this... here.
After much research, I'm not sure that this is possible - it would be best to write a screen saver, implement it's own password protection, and enable that dialog to be modified from behind the screen saver.

Application does not accept keystroke

I am trying to send keystrokes to the application VisualBoyAdvance using AppleScript, but I cannot get it to work.
My code, so far, is this:
tell application "VisualBoyAdvance"
activate
tell application "System Events"
keystroke "k"
end tell
end tell
When I tell VisualBoyAdvance directly, I get this error:
error "VisualBoyAdvance got an error: Can’t get keystroke \"k\"." number -1728 from keystroke "k"
I have tried telling VisualBoyAdvance directly, and I have also tried using key code 40, but I still cannot get it to work. Strangely enough, this does work:
tell application "VisualBoyAdvance"
activate
tell application "System Events"
keystroke "d" using {command down}
end tell
end tell
But that is a keyboard shortcut that shows up in the menu bar, so I guess it would be a bit different.
How can I use AppleScript to simulate a keypress and make the application respond to it? If I cannot use AppleScript for this, what else could I use?
I think you're almost there. Here's something I used for Safari; in this example, I send key code 48 (tab).
tell application "Safari"
activate
tell application "System Events" to tell process "Safari" to key code 48
end tell
AFAICS this ought to be largely independent of AppleScript support in the target process as you're asking System Events to simulate the key press via Universal Access.
For help with key codes, see this useful application: http://manytricks.com/keycodes/
It's the developer's choice to make an application fully Applescript aware. Menu items are Applescriptable from the Finder's point of view, but other UI options may or may not be. See UIElementInspector to examine that application for scriptable elements.
I can't garuntee anything as I don't have this app but here is some things to try
tell application "VisualBoyAdvance"
activate
tell application "System Events"
tell application process "VisualBoyAdvance"
try
keystroke "k"
on error
try
keystroke (ASCII character 75)
end try
end try
end tell
end tell
end tell