Topic: https://brettterpstra.com/2011/01/22/quick-tip-applescript-application-toggle/
hide preview

What's next? verify your email address for reply notifications!

Ric Zito 13y, 118d ago

Sorry - I meant ctrl-H and ctrl-opt-H...

hide preview

What's next? verify your email address for reply notifications!

Ric Zito 13y, 118d ago

Hi there,

I'm coming to this thread very late, but I find it interesting because I too use Spark as my go-to hotkey app, and have done for years. It still works great, even on Lion. I just thought you might like to know that Spark has built in "Hide foreground" and "Hide others" actions : no need for Applescript or anything similar. It's global (if you want it to be) so it works in ANY app, and plus, it's much faster than Applescript.

In Spark's main window, just dbl-click "Application" in the sidebar, and in the slide-dwon sheet that appears, drop down the Action menu. There you'll find the commands. I've set cmd-H and cmd-opt-H as my hotkeys. It works an absolute treat.

cheers,
Ric

hide preview

What's next? verify your email address for reply notifications!

Jason Clarke 14y, 52d ago

It would be fantastic to be able to set global hotkeys to jump to specific tabs in Chrome. My first 3 tabs in Chrome are Personal Gmail, Work Gmail, and FogBugz. I'd love the ability to jump directly to one of those three using a keyboard shortcut.

It seems to me that it should be possible to modify the application toggle script to add a "command-1" sent to the app once it displays. That would solve the problem for me. It seems trivially easy, but my experience is that things that seem like easy changes rarely are.

I'm trying to use variations of:

tell application "System Events" to keystroke "1" using command down

in various spots in the script, but in testing it doesn't ever seem to respond by switching the active tab.

Any chance someone reading this would be able to help me with how to do this?

hide preview

What's next? verify your email address for reply notifications!

Jason Clarke 14y, 54d ago

Seriously Brett, this is all kinds of awesome. This is like the umpteenth time I've wished for a feature of some kind, only to have it solved by what is for you, merely a "quick tip".

hide preview

What's next? verify your email address for reply notifications!

Zettt 14y, 58d ago

Final solution. Don't know whether this is better than other solutions. I guess it is one way to do it ...

set appName to "Safari"
set alreadyLaunched to false

tell application "System Events"
if not (exists process appName) then
set alreadyLaunched to false
else
set alreadyLaunched to true
end if
set frontmostApp to name of the first process whose frontmost is true
end tell

if alreadyLaunched is false then
tell application appName to launch
else if alreadyLaunched is true then
if frontmostApp is equal to appName then
tell application "System Events" to set visible of process appName to false
else
tell application appName to activate
end if
end if
remark link
hide preview

What's next? verify your email address for reply notifications!

D Curtis 14y, 57d ago

@Zett activate will bring the app to the front whether it is open or not. So really, all you need to do is check if the app you're looking for is frontmost. If it is, you hide it. If it isn't, you activate it. Ideally this should work but I don't care enough anymore to test it.

set appName to "Mail"
set needsActivation to false
tell application "System Events"
if frontmost of process appName then
set visible of process appName to false
else
set needsActivation to true
end if
end tell

if needsActivation then
tell application appName to activate
end if
hide preview

What's next? verify your email address for reply notifications!

Zettt 14y, 58d ago

Interesting problem. The solution is a bit awkward, but I got it working. (Launch By Creator or Identifier )

set appName to bundle identifier of (info for (path to application "Mail"))
hide preview

What's next? verify your email address for reply notifications!

D Curtis 14y, 58d ago

This is a very weird bug. Seems like it just doesn't want to work inside the
System Events tell. If you do the following it works. It's kinda ugly but I
think it's because if you do a tell inside another tell then the second
goes through the first. So in the original you're telling System Events to
tell Mail to activate.

set appName to "Mail"
set startIt to false
tell application "System Events"
if not (exists process appName) then
set startIt to true
else if frontmost of process appName then
set visible of process appName to false
else
set frontmost of process appName to true
end if
end tell
if startIt then
tell application appName to activate
end if

But what's even more weird is that this works,

set appName to "Mail"
set startIt to false
tell application "System Events"
if not (exists process appName) then
tell application "Mail" to activate
else if frontmost of process appName then
set visible of process appName to false
else
set frontmost of process appName to true
end if
end tell
if startIt then
tell application appName to activate
end if

but this does not work as you probably already know,

set appName to "Mail"

tell application "System Events"
if not (exists process appName) then
tell application appName to activate
else if frontmost of process appName then
set visible of process appName to false
else
set frontmost of process appName to true
end if
end tell

Also you can use exists instead of name of every
process ..
.

hide preview

What's next? verify your email address for reply notifications!