Jump to content

Need to have "System Events" running before I can access menu bar 2 properly


Recommended Posts

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"

    activate
end 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

 

 

 

Link to comment

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!

Link to comment

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

Link to comment

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
Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...