strug Posted January 12, 2016 Posted January 12, 2016 Hi there, I'm absolutely new to applescript and writing alfred workflows. I want to write a workflow to connect to VPN with "Pulse Secure". Therefore I want to select the menu bar icon and select a connection. I managed to get it working when "System Events" is already running: on alfred_script(query) set network to "work network" if query is not "" then set network to query end if ignoring application responses tell application "System Events" to tell process "Pulse Secure" click menu bar item 1 of menu bar 2 end tell end ignoring do shell script "killall System\\ Events" delay 0.2 tell application "System Events" to tell process "Pulse Secure" tell menu bar item 1 of menu bar 2 tell menu item network of menu 1 click tell menu item "Connect" of menu network click end tell end tell end tell end tell end alfred_script If "System Events" is not running this script fails. I tried a couple of ways to start "System Events" if it's not running, but even with a delay afterwards it fails. I tried it with tell application "System Events" activateend tell with launch "System Events" delay 2 with http://apple.stackexchange.com/questions/95834/why-does-this-applescript-often-fail-the-first-time-but-always-work-after-that and a couple of other combinations. Unfortunately without success:( Hopefully you can help me a bit. Best regards
deanishe Posted January 13, 2016 Posted January 13, 2016 Have you tried it without the line ignoring application responses and without killing System Events? This all sounds extremely strange. Any application you specify in a tell application block is started if it isn't already running.
strug Posted January 14, 2016 Author Posted January 14, 2016 Without ignoring the first menu opens but waits for ~7seconds, after that the submenu is opened and the entry get selected. No matter if System Events was running or not. To me it seems as if my script requires "System Events" to be running in order to run properly/quick. If it is not running, it will be started (I see that in the Activity Monitor), but my script did not wait for that (because of the ignore;) so that in that case it fails with a timeout/race condition. Any idea how I can speed up the workflow when removing the ignore? Thanks a lot!
deanishe Posted January 14, 2016 Posted January 14, 2016 I don't really understand what it is you're trying to achieve. Most of all, I do not understand the purpose of calling System Events but ignoring it, and then killing it, and then calling it again. Why are you killing System Events?
strug Posted January 14, 2016 Author Posted January 14, 2016 If I don't ignore and kill that I have to wait ~7 seconds between open the menu bar 2 menu and selecting the submenu. I found somebody having a similar problem and his solution was to kill the "waiting". This works when System Events is already running, but not, when not. So if I remove the ignoring and kill stuff the workflow works, but have a ~7seconds pause before the submenu is selected. I tried to combine it: tell application "System Events" to tell process "Pulse Secure" click menu item "Connect" of menu network of menu item network of menu bar item 1 of menu bar 2 end tell but so far I couldn't manage it to work: [ERROR: alfred.workflow.action.applescript] { NSAppleScriptErrorAppName = "System Events"; NSAppleScriptErrorBriefMessage = "Can\U2019t get menu item \"work network\" of menu bar item 1 of menu bar 2 of process \"Pulse Secure\"."; NSAppleScriptErrorMessage = "System Events got an error: Can\U2019t get menu item \"work network\" of menu bar item 1 of menu bar 2 of process \"Pulse Secure\"."; NSAppleScriptErrorNumber = "-1728"; NSAppleScriptErrorRange = "NSRange: {499, 95}"; } Thanks so far
strug Posted January 14, 2016 Author Posted January 14, 2016 Here is the link to the one suggesting to ignore/kill the System Events: http://stackoverflow.com/questions/16492839/applescript-on-clicking-menu-bar-item-via-gui-script
deanishe Posted January 15, 2016 Posted January 15, 2016 Right. I think I follow you now. The reason your script fails when System Events isn't running is because your do shell script "killall System\\ Events" command fails. If you run killall on a non-existent process, it exits with an error and your AppleScript then also exits with an error.Just wrap that line in a try block to ignore the error: try do shell script "killall System\\ Events" end try
strug Posted January 19, 2016 Author Posted January 19, 2016 mmh, does not work properly either. I saw that the flux workflow (http://www.packal.org/workflow/flux-control-plus-remote) do s.th. similar, but this workflow doesn't work on my machine either.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now