ylluminate
ylluminate

Reputation: 12369

How to send a menubar app (with no menubar menu items) a command in AppleScript?

I would like to control an application (Shady.app) with an AppleScript command. Unfortunately it doesn't have the standard menubar items such as File, Edit, View, etc.

My goal is to somehow issue a command to the app through AppleScript such that it will toggle the "Turn Shady On" & "Turn Shady Off" command:

Shady App

NOTE: "Turn Shady On" is the OFF state and it turns to "Turn Shady Off" when Shady is in the ON state.

How can you create a script that toggles this both directions based on it's current state in AppleScript? Or is there a better way than AppleScript to control this programmatically?

Upvotes: 2

Views: 1090

Answers (3)

wch1zpink
wch1zpink

Reputation: 3142

This AppleScript will "Turn Shady Off"

tell application "System Events"
    try
        click menu item "Turn Shady Off" of menu 1 of menu bar item "Shade" of menu bar 1 of application process "Shady"
        click menu bar item 1 of menu bar 2 of application process "Shady"
    end try
end tell

This AppleScript will "Turn Shady On"

tell application "System Events"
    try
        click menu item "Turn Shady On" of menu 1 of menu bar item "Shade" of menu bar 1 of application process "Shady"
        click menu bar item 1 of menu bar 2 of application process "Shady"
    end try
end tell

Assuming it's being run in the status bar.

This will toggle on and off

on toggle()
    tell application "System Events"
        try
            click menu item "Turn Shady On" of menu 1 of menu bar item "Shade" of menu bar 1 of application process "Shady"
        end try
        try
            click menu item "Turn Shady off" of menu 1 of menu bar item "Shade" of menu bar 1 of application process "Shady"
        end try
        click menu bar item 1 of menu bar 2 of application process "Shady"
    end tell
end toggle

toggle()

Save it as an application In ScriptEditor or as a service in Automator or whatever you choose. No need for FastScripts.

Upvotes: 2

GDP2
GDP2

Reputation: 2018

You can do something like this via Keyboard Maestro. Here's an image demonstrating what you want in action:

enter image description here

For the record, I also thought this was so awesome that I also included a way to create a darken/lighten global shortcut:

enter image description here

enter image description here

You've also gotta have "Show Dock Icon" turned on in system preferences for this to work!

See this post on the forums for more info. I'll go ahead and paste it here as well for convenience's sake and to circumvent link rot:


sonicly:

I have an application that runs only as a menubar app. It has a simple command that is essentially "Turn App On"/"Turn App Off". Is there a way to make Keyboard Maestro set up a global hotkey to initiate the command in this app?

I just ran across this topic which addresses your issue: osx - How to send a menubar app (with no menubar menu items) a command in AppleScript? - Stack Overflow

I don't know if you are the OP or not, and I have NOT tested the suggested solution. But maybe it will at least get you started.

sonicly:

So I can enable it to show up on the Dock and with that I was able to set keyboard shortcuts that work... But I have to get focus on the app first (i.e., cmd-tab). That's a hassle and I'd rather just issue something like command-option-shift-space to turn it on and then to toggle it back off again. That seems doable with KM, but I'm not sure how to make the Maestro do that per se

This is very easy in KM, just use the

Another option is to use the Select or Show a Menu Item action (KM Wiki), but I'm not sure if it will work in this situation.

Upvotes: 0

l'L'l
l'L'l

Reputation: 47169

The standard way to send a command via short-cut is by:

System Preferences > Keyboard > Shortcuts > App Shortcuts

enter image description here

Click the + and select Shady, then you'll be able to define the short-cut you want to use. Keep in mind you'll need to use + Tab to switch to Shady first, then use the short-cut.

If you use an application like FastScripts then you can create a global short-cut without having to switch to the application first which is sometimes better if the app doesn't have focus.

Upvotes: 1

Related Questions