Jump to content

AppleScript works in Script Editor, but doesn't in Alfred


Recommended Posts

I have an AppleScript that presses the 'Connect to...' button under the menu bar icon of my VPN client.

tell application "System Events"
	if exists process "IVPN" then
		tell process "IVPN" to tell menu bar item 1 of menu bar 2
			ignoring application responses
				click
			end ignoring
		end tell
	end if
end tell

do shell script "killall System\\ Events"
delay 0.1

tell application "System Events"
	if exists process "IVPN" then
		tell process "IVPN" to tell menu bar item 1 of menu bar 2
			tell menu 1
				if exists (first menu item whose title starts with "Connect to") then
					click (first menu item whose title starts with "Connect to")
				else
					key code 53
				end if
			end tell
		end tell
	end if
end tell

If there is no such button (already connected), then the escape keystroke is sent (to close the menu). Due to the 5s delay after the first click (a known UI scripting behavior), I had to add a workaround that ignores application response on the first click and then kills all instances of 'System Events' via a shell command, and then clicks the actual button.

 

This setup works consistently when launched from the Script Editor.

 

However, in Alfred it just doesn't. There are no errors in the debug console, and, judging by the bell sound, the script always ends up sending the key code 53 (pressing Esc).

 

What exactly could be the problem?

Link to comment

Welcome @mort_the_swift,

 

When asking for help with a Workflow, please upload it somewhere (transfer.sh is a fast solution without ads; drop your Workflow in the box in the middle) as we can’t properly help you without access to it. Debugging can already be hard with access to the code, and you’re asking us to guess yours from a description. There are multiple places where your code or Workflow setup may be going wrong. Without looking at it we’re shooting in the dark.


Read the Reporting Problems with Workflows topic, as it gives a nice overview on how to build an effective report.

Link to comment
  • 2 weeks later...

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...