Jump to content

Delay in AppleScript based workflow


Recommended Posts

I am trying to do the following task using a workflow. I have a workflow called get things done where I set my status in Microsoft Lync to Busy and would like to set the status back to 'Available' after some amount of time. I got this to work for setting my status to busy, however the delay causes me to no longer be able to use the hotkey to access alfred. It appears that alfred is blocked running the delay I added to my script. Anyone have a more standard way of doing this?

-- Save your current application
tell application "System Events"
	set currentApp to name of 1st process whose frontmost is true
end tell

-- Bring Lync to the front so we can use the menu
tell application "Microsoft Lync"
	activate
end tell

-- Set your status to 'Available'
tell application "System Events"
	tell process "Microsoft Lync"
		tell menu bar 1
			tell menu bar item "Status"
				tell menu "Status"
					click menu item "Busy"
				end tell
			end tell
		end tell
	end tell
end tell

-- Return to your previous application
tell application currentApp
	activate
end tell

delay 60 * 60

-- Save your current application
tell application "System Events"
	set currentApp to name of 1st process whose frontmost is true
end tell

-- Bring Lync to the front so we can use the menu
tell application "Microsoft Lync"
	activate
end tell

-- Set your status to 'Available'
tell application "System Events"
	tell process "Microsoft Lync"
		tell menu bar 1
			tell menu bar item "Status"
				tell menu "Status"
					click menu item "Available"
				end tell
			end tell
		end tell
	end tell
end tell

-- Return to your previous application
tell application currentApp
	activate
end tell
Link to comment

The obvious cause would be that you're running your AppleScript as a Run NSAppleScript Action. When you run a script this way, Alfred blocks till the script exits.
 
 
Instead, you should use a normal Run Script Action and set language to /usr/bin/osascript (AS). This way, the script is run in the background and you can continue to use Alfred while it's running.

Link to comment

Worked like a charm! Thanks.

set temp to {query} as integer

-- Save your current application
tell application "System Events"
	set currentApp to name of 1st process whose frontmost is true
end tell

-- Bring Lync to the front so we can use the menu
tell application "Microsoft Lync"
	activate
end tell

-- Set your status to 'Busy'
tell application "System Events"
	tell process "Microsoft Lync"
		tell menu bar 1
			tell menu bar item "Status"
				tell menu "Status"
					click menu item "Busy"
				end tell
			end tell
		end tell
	end tell
end tell

-- Return to your previous application
tell application currentApp
	activate
end tell

delay temp * 60

-- Save your current application
tell application "System Events"
	set currentApp to name of 1st process whose frontmost is true
end tell

-- Bring Lync to the front so we can use the menu
tell application "Microsoft Lync"
	activate
end tell

-- Set your status to 'Available'
tell application "System Events"
	tell process "Microsoft Lync"
		tell menu bar 1
			tell menu bar item "Status"
				tell menu "Status"
					click menu item "Available"
				end tell
			end tell
		end tell
	end tell
end tell

-- Return to your previous application
tell application currentApp
	activate
end tell


on delay duration
  set endTime to (current date) + duration
  repeat while (current date) is less than endTime
    tell AppleScript to delay endTime - (current date)
  end repeat
end delay
Edited by rocketcity
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...