AppleScript: keystroke 125 (down key) on Mavericks - applescript

In Mac Lion: choose lock screen in lockscreen icon (Keychain Access -> Preferences --> General -> Show keychain ...), it will in screen saver mode.
In mavericks: when lock screen by above steps, it is NOT in screen saver mode. So we can not control it. I don't know why???
Then in locked screen, I tranfer event to press down key On MAVERICKS (It's OK on Mac Lion), but it seems not know about that:
on run
tell application "/System/Library/Frameworks/ScreenSaver.framework/Versions/A/Resources/ScreenSaverEngine.app" to quit
tell application "System Events" to key code 125
delay 1
tell application "System Events" to keystroke "password"
delay 1
tell application "System Events" to keystroke return
delay 5
end run
Excute above script by:
NSString *source = [NSString stringWithFormat:kAppleScriptUnlockWithPassword ,password];
// Create unlock script with password
NSAppleScript* appleScript = [[NSAppleScript alloc] initWithSource:source];
NSDictionary * errDic = nil;
// Execute script
[screenState removeAllObjects];
[appleScript executeAndReturnError:&errDic];
NSLog(#"%#", errDic);
It returns:
{
NSAppleScriptErrorAppName = "System Events";
NSAppleScriptErrorBriefMessage = "Can\U2019t make {1, \"\"} into type text.";
NSAppleScriptErrorMessage = "System Events got an error: Can\U2019t make {1, \"\"} into type text.";
NSAppleScriptErrorNumber = "-1700";
NSAppleScriptErrorRange = "NSRange: {92, 20}";
}
Do you have any idea about it?
Please help me.
Thanks

keystroke 125 inserts 125 as text. Try using key code instead:
quit application "ScreenSaverEngine"
tell application "System Events"
key code 125
delay 1
keystroke "password" & return
end tell

Related

how to make the element selection to return null instead of throw error in applescript

I am debugging a legacy desktop automation tool which use AppleScript for UI control on MacOS. One of the script keeps failing with error "System Events got an error: Can’t get static text \"2\" of window \"UISoup – mac_utils.py\" of application process \"pycharm\"." number -1728 from static text "2" of window "UISoup – mac_utils.py" of application process "pycharm", and here is the script
tell application "System Events" to tell process "PyCharm"
set visible to true
set uiElement to static text "2" of window "UISoup – mac_utils.py" of application process "pycharm" of application "System Events"
set layer to 1
if uiElement = null then
set layer to 0
set collectedElements to {UI elements of front window, layer}
else
set layer to layer + 1
set collectedElements to {null, layer}
if name of attributes of uiElement contains "AXChildren" then
set collectedElements to {value of attribute "AXChildren" of uiElement, layer}
end if
end if
end tell
It seems like this line set uiElement to static text "2" of window "UISoup – mac_utils.py" of application process "pycharm" of application "System Events" should set the uiElement to null if the element is not found. But in fact it will throw error instead. How to let it return null instead of throw error to make it works? Thank you.
In the AppleScript equivalent of null is missing value. When GUI scripting, the process should be frontmost. You can check existing of UI elements using command exists (you can use try block as well).
tell application "System Events" to tell process "PyCharm"
set frontmost to true
-- set visible to true
set uiElementExists to exists static text "2" of window "UISoup – mac_utils.py"
set layer to 1
if not uiElementExists then
set layer to 0
set collectedElements to {UI elements of front window, layer}
else
set layer to layer + 1
set collectedElements to {missing value, layer}
set uiElement to static text "2" of window "UISoup – mac_utils.py"
if name of attributes of uiElement contains "AXChildren" then
set collectedElements to {value of attribute "AXChildren" of uiElement, layer}
end if
end if
end tell

How to invert colors with Applescript?

I was doing some research on how to invert colors with Applescript. For the most part, I just found stuff like:
tell application "System Events"
keystroke "8" using {command down, option down, control down}
end tell
But on Yosemite (And I think versions before this), this won't work. You have to go into system preferences and click around in there. So, is there a way to make an applescript that would invert colors?(Preferably without having to "Click" anything because that requires accessibility rights for the script)
You can achieve this by using the 'do shell script' command in Applescript, using this:
do shell script "sudo defaults write /Library/Preferences/.GlobalPreferences AppleInterfaceTheme Dark"
That should turn on "dark mode" in OS X Yosemite. Cheers!
In Yosemite JXA Javascript (should be readily adaptable to AS if you prefer)
toggling dark theme and desktop color together:
function run() {
var dctSettings = {
darkMode: {
day: true,
night: false
},
background: {
day: "/Library/Desktop Pictures/Solid Colors/Solid Gray Pro Ultra Dark.png",
night: "/Library/Desktop Pictures/Solid Colors/Solid Gray Light.png"
}
},
strMode;
app = Application("System Events");
if (app.currentDesktop.picture() === dctSettings.background.day) {
app.currentDesktop.picture = dctSettings.background.night;
app.appearancePreferences.darkMode = dctSettings.darkMode.night;
strMode = "Night";
} else {
app.currentDesktop.picture = dctSettings.background.day;
app.appearancePreferences.darkMode = dctSettings.darkMode.day;
strMode = "Day";
}
return strMode;
}
tell application "System Events"
key code 28 using {command down, option down, control down}
end tell
It's 28 and not 8, Key code 28 is for the "8" key on the main part of the keyboard.

The variable result is not defined AppleScript

I am working on the same app that I mentioned in my first question. I have gotten much farther, but when I try to play "2048" the first time, AppleScript give me the error:
"The variable result is not defined"
I will skip the main bulky body of the app and get to the area with the problem:
display dialog "What would you like to do?
Sleep = Go to sleep
Finder = Open Finder
Time = Display current time and date
2048 = play 2048
Quit = Quit application" default answer "" with title "Control panel"
if the text returned of the result is "Sleep" then
tell application "System Events" to sleep
display dialog "Hello, welcome back!" buttons {"Thank you!"} default button 1
return
else if the text returned of the result is "Finder" then
tell application "Finder" to make new Finder window
else if the text returned of the result is "Time" then
set a to (current date) as list
set AppleScript's text item delimiters to linefeed
set a to a as text
display dialog a buttons {"OK"} default button 1
else if the text returned of the result is "Quit" then
return
else if the text returned of the result is "2048" then
tell application "Terminal"
do script "/Users/student/Documents/2048.sh"
activate
end tell
else
display dialog "Invalid response" with title "Invalid response" buttons {"Go back", "Quit"} default button 1
end if
if the button returned of the result is "Go back" then
display dialog "What would you like to do?
Sleep = Go to sleep
Finder = Open Finder
Time = Display current time and date
2048 = play 2048
Quit = Quit application" default answer "" with title "Control panel"
else
return
end if
if the text returned of the result is "Sleep" then
tell application "System Events" to sleep
display dialog "Hello, welcome back!" buttons {"Thank you!"} default button 1
return
else if the text returned of the result is "Finder" then
tell application "Finder" to make new Finder window
else if the text returned of the result is "Time" then
set a to (current date) as list
set AppleScript's text item delimiters to linefeed
set a to a as text
display dialog a buttons {"OK"} default button 1
else if the text returned of the result is "Quit" then
return
else if the text returned of the result is "2048" then
tell application "Terminal"
do script "/Users/student/Documents/2048.sh"
activate
end tell
end if
Just set the result of the display dialog to a variable and use the variable. It's tricky to use "result" as you have in many if statements. One of them is bound to cause a problem because "result" will change at some point.
So do something like this right after your display dialog statement...
set {buttonReturned, textReturned} to {button returned of result, text returned of result}
Then in your if statements use buttonReturned or textReturned.

How to insert text at cursor position of another application on Mac OSX Application (like OSK)?

I'm trying to create an OSX application which would be a replica of On Screen keyboard. Is it possible to insert some text into the cursor position of another active application? Thanks in advance!
It is possible. Accessibility enables to change content of other applications, but your app can't be sandboxed and thus not available via AppStore.
CFTypeRef focusedUI;
AXUIElementCopyAttributeValue(AXUIElementCreateSystemWide(), kAXFocusedUIElementAttribute, &focusedUI);
if (focusedUI) {
CFTypeRef textValue, textRange;
// get text content and range
AXUIElementCopyAttributeValue(focusedUI, kAXValueAttribute, &textValue);
AXUIElementCopyAttributeValue(focusedUI, kAXSelectedTextRangeAttribute, &textRange);
NSRange range;
AXValueGetValue(textRange, kAXValueCFRangeType, &range);
// replace current range with new text
NSString *newTextValue = [(__bridge NSString *)textValue stringByReplacingCharactersInRange:range withString:newText];
AXUIElementSetAttributeValue(focusedUI, kAXValueAttribute, (__bridge CFStringRef)newTextValue);
// set cursor to correct position
range.length = 0;
range.location += text.length;
AXValueRef valueRef = AXValueCreate(kAXValueCFRangeType, (const void *)&range);
AXUIElementSetAttributeValue(focusedUI, kAXSelectedTextRangeAttribute, valueRef);
CFRelease(textValue);
CFRelease(textRange);
CFRelease(focusedUI);
}
This code does not check for errors and makes assumption that focused element is text area.
Not sure if this helps, but in Applescript you can do something like
tell application "System Events"
keystroke "Stuff here"
end tell
So maybe you can try call that within cocoa using NSApplescript or using NSTask and run
osascript -e 'tell application "System Events" to keystroke "Stuff here"'

Applescript for safari to click downloads

I have been trying to write some applescript that checks to see if the Downloads window is open in Safari and if it is to click and open the last file in the list which is the last file that was downloaded but have been having some issues using Accessibility Inspector I get the following :
<AXApplication: "Safari">
<AXWindow: "Downloads">
<AXScrollArea>
<AXList>
<AXGroup: "ExcelTest.xls">
<AXButton: "file icon">
Attributes:
AXRole: "AXButton"
AXRoleDescription: "button"
AXHelp: "Open"
AXFocused: "false"
AXParent: ""
AXWindow: ""
AXTopLevelUIElement: ""
AXPosition: "x=1062 y=396"
AXSize: "w=32 h=32"
AXDescription: "file icon"`
AXEnabled: "true"
Actions:
AXPress - press
I'm not sure how to access the scroll area and list to get at the button.
This works...
set downloadsIsFrontmost to false
tell application "Safari"
set theWindows to name of windows
if (item 1 of theWindows) is "Downloads" then
activate
set downloadsIsFrontmost to true
end if
end tell
if downloadsIsFrontmost then
tell application "System Events"
tell process "Safari"
set theGroups to groups of list 1 of scroll area 1 of window "Downloads"
set lastGroup to last item of theGroups
repeat 2 times
click button 1 of lastGroup
delay 0.05
end repeat
end tell
end tell
end if

Resources