Jump to content

[SOLVED] Open an application and do an action from menu of that application


Recommended Posts

Is there a simple way of opening an app and execute its menu command using Alfred ?

 

For example, in Lepton app the keyboard combination `shift-space` will do the gists search. I would like to 

make an alfred workflow to do this search.

 

Attempt:

1.  Assign a hotkey 

2. Lauch app Lepton

3. Key combo

 

However, this will only open the app and does not run the key combo.

 

Is there a simple AppleScript to do this? Or, am I doing something wrong here?

 

 

 

 

lepton.png

Link to post
31 minutes ago, Bhishan said:

Or, am I doing something wrong here?

 

You’re firing the Key Combo too soon, before the app has gotten the time to open. Insert a delay before opening the app. Or use AppleScript to launch the app (tell application "Lepton" to activate) as that’ll only return after the app is finished launching.


You may also use AppleScript to activate the specific menu item, instead of relying on keyboard shortcuts. I’ll leave that as an exercise to you, if you wish to go that route.

Link to post

Thanks a lot @vitor

 

I inserted an

Utilites > Delay 

With 2 seconds, it worked for now.

 

My attempt with AppleScript

on alfred_script(q)
  tell application "Lepton"

activate

end tell


tell application "System Events"

tell process "Lepton"

click menu item "Search" of menu "Gist" of menu bar 1

end tell

end tell

end alfred_script

 

And, this works. Thanks a lot!

Edited by Bhishan
Link to post

@Bhishan 

Unless you specifically need it, avoid Run NSAppleScript in favour of Run Script with /usr/local/osascript (AS).


Also, if you’re only giving one command to tell, use to in one line instead of end tell in a block. So

tell application "Lepton"
  activate
end tell

becomes

tell application "Lepton" to activate

The whole script can be done in two lines

tell application "Lepton" to activate
tell application "System Events" to tell process "Lepton" to click menu item "Search" of menu "Gist" of menu bar 1

 

Edited by vitor
Link to post
  • vitor changed the title to [SOLVED] Open an application and do an action from menu of that application

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