Skip to content

Instantly share code, notes, and snippets.

@fuji246
Created March 19, 2020 19:03
Show Gist options
  • Save fuji246/33121095c5ddb9c195c972c0e6ae6914 to your computer and use it in GitHub Desktop.
Save fuji246/33121095c5ddb9c195c972c0e6ae6914 to your computer and use it in GitHub Desktop.
application control applescript template
#!/usr/bin/osascript
on getProcessName(appPath)
set AppleScript's text item delimiters to "/"
set filename to text item -1 of appPath
set AppleScript's text item delimiters to "."
set processName to text item -2 of filename
log "processName:" & processName
return processName
end getProcessName
on getappPath()
set appPath to (system attribute "APP_HOME")
if appPath is equal to ""
set appPath to POSIX path of (choose file with prompt "----------Please choose YourAppName.app-------------" of type {"app"})
end if
log "appPath: " & appPath
return appPath
end getappPath
on dostart(appPath)
set addArgs to (system attribute "APP_ARGS")
log "args: " & addArgs
do shell script "open -n -a '" & appPath & "' --args " & addArgs
tell application "Finder"
set {0, 0, dtw, dth} to bounds of window of desktop
end tell
if application "Terminal" is running then
tell application "System Events" to tell process "Terminal"
set visible to false
end tell
end if
#set processName to getProcessName(appPath)
tell application "System Events"
#tell process processName
tell (first process whose frontmost is true)
activate
delay 1
#click button "Connect" of window "YourAppName"
#click button "Statistics Info" of window "YourAppName"
#set statsWindow to window 1
click (button 1 where subrole is "AXMinimizeButton") of window "YourAppName"
if (window "Video Window" exists) and (windows "View host's screen sharing" exists) then
set position of windows "Video Window" to {0, 0}
set size of windows "Video Window" to {dtw/2, dth}
set position of windows "View host's screen sharing" to {dtw/2, 0}
set size of windows "View host's screen sharing" to {dtw/2, dth}
else if (window "Video Window" exists) then
set position of windows "Video Window" to {0, 0}
set size of windows "Video Window" to {dtw, dth}
else if (windows "View host's screen sharing" exists) then
set position of windows "View host's screen sharing" to {0, 0}
set size of windows "View host's screen sharing" to {dtw, dth}
end if
#set windowSize to size of statsWindow
#set windowWidth to item 1 of windowSize
#set windowHeight to item 2 of windowSize
#set position of statsWindow to {(dtw-windowWidth)/2, (dth-windowHeight)/2}
end tell
end tell
end dostart
on dostop(appPath)
set processName to getProcessName(appPath)
tell application "System Events"
tell process processName
activate
#click button "Disconnect" of window "YourAppName"
end tell
end tell
tell application processName to quit
end dostop
on run argv
set helpStr to "Usage: appctrl.applescript[(start|stop)]
params not supported yet, only for pcap replay
"
set action to "none"
if length of argv is not less than 1 then
set action to item 1 of argv
else
return helpStr
end if
# user need to set APP_HOME to avoid choosing the app path
# export APP_HOME="/Application/YourAppName.app"
set appPath to getappPath()
if action is equal to "start" then
dostart(appPath)
else if action is equal to "stop" then
dostop(appPath)
else
return helpStr
end if
end run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment