Mac OS programming¶
AppleScript Programming/System Events¶
https://en.wikibooks.org/wiki/AppleScript_Programming/System_Events
Loop (repeat) - http://alvinalexander.com/apple/applescript-for-loop-while-loop-examples
Get applescript script filename function:
on getScriptFileName()
set myPath to path to me as text
if myPath ends with ":" then
set n to -2
else
set n to -1
end if
set AppleScript's text item delimiters to ":"
return text item n of myPath
end getScriptFileName
# Will return '<name>.<extension>'
# Using System Events
on getScriptFileName()
tell application "System Events"
return name of (path to me)
end tell
end getScriptFileName
# Will return '<name>.<extension>'
Check is application running:
if application "Messages" is running then
log "is running"
end if
Activate application:
tell application "Terminal"
activate
end tell
# using osascript
osascript -e 'tell application "Terminal" to activate'
Check if static text start with:
if value of static text 1 of group 2 of group 1 of UI element 1 of scroll area 1 does not start with "Enter the verification code" then
error "Expected label does not exist."
end if
Check multiple elements exists:
if (static text "bla bla" exists) and (static text "yeah" exists) and (text field 1 exists) then
log "true"
else
log "false"
end if
Check if button not exists:
if not exists (button "Verify") then
error "Verify buttonl does not exist."
end if
Get all UI elements:
# my showElements(element, "test")
# showElements(element, "test")
on showElements(element, id)
log "-" & id & "-"
tell application "System Events"
set elements to get every UI element of element
repeat with el in elements
log el
end repeat
end tell
log "-" & id & "-"
end showElements
# my showAllElements(element)
on showAllElements(element)
tell application "System Events"
set elements to get every UI element of element
repeat with el in elements
log el
my showAllElements(el)
end repeat
end tell
end showElements
Show all processes of application:
tell application "Finder"
set l to get name of every process
repeat with i in l
log i
end repeat
end tell
Pass command-line arguments to AppleScript osascript script.scpt hello world
. script.scpt
:
on run argv
return item 1 of argv & item 2 of argv
end run
Debug UI elements (dirty):
#!/usr/bin/osascript
tell application "Messages"
activate
end tell
tell application "System Events"
get properties
if UI elements enabled then
tell process "Messages"
set t1 to get every UI element
log t1 & "\n"
tell window "Messages"
set t1 to get every UI element
log t1 & "\n"
tell sheet 1
set t1 to get every UI element
log "get every UI element"
log t1 & "\n"
tell scroll area 1
set t1 to get every UI element
log "get every UI element"
log t1 & "\n"
tell UI element 1
set t1 to get every UI element
log "get every UI element"
log t1 & "\n"
tell group 1
set t1 to get every UI element
log "get every UI element"
log t1 & "\n"
tell group 2
set t1 to get every UI element
log "get every UI element"
log t1 & "\n"
tell static text "Apple ID Locked"
if not (exists) then
log "FALSE"
else
log "TRUE"
end if
end tell
end tell
end tell
end tell
set t1 to get every button
log t1 & ".\n"
end tell
set t1 to get every static text
log t1 & "\n"
end tell
set t1 to get every button
log t1 & "\n"
set t1 to get properties of every button
log t1 & "\n"
set t1 to get every UI element of every button
log t1 & "\n"
set t1 to get every static text
log t1 & "\n"
set t1 to get properties of every static text
log t1 & "\n"
set t1 to get every UI element of every static text
log t1 & "\n"
set t1 to get every scroll bar
log t1 & "\n"
get properties of every scroll bar
get every UI element of every scroll bar
(*get every UI element ¬
whose class is not button and class is not static text ¬
and class is not scroll bar
get properties of every UI element ¬
whose class is not button and class is not static text ¬
and class is not scroll bar*)
end tell
end tell
else
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.universalaccess"
display dialog "UI element scripting is not enabled. Check \"Enable access for assistive devices\""
end tell
end if
end tell
/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -switchToUserID ${uid}
osascript << EOF
tell application "System Events"
delay 1.0
keystroke "${password}" & return
end tell
EOF
Open new tab in terminal and run command:
osascript << EOF
tell application "Terminal"
activate
tell application "System Events" to keystroke "t" using command down
repeat while contents of selected tab of window 1 starts with linefeed
delay 0.01
end repeat
do script "echo a" in window 1
end tell
EOF
Run command in new or existing terminal an close it:
osascript << EOF
tell application "Terminal"
activate
do script "echo a" in window 1
quit
end tell
EOF