Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

wrignj08

macrumors newbie
Original poster
Jun 13, 2009
28
0
Hey all, i need an applescript that will activate an app if it is not currently open or if it is open quit it (so basically the script will act like an activation switch). any suggestions thanks.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Code:
[b]set[/b] [color=#408000]myAppName[/color] [b]to[/b] "Stickies"
[b]set[/b] [color=#408000]isRunning[/color] [b]to[/b] [color=#4a1e7f]false[/color]
[b]tell[/b] [i][color=#0000ff]application[/color][/i] "System Events"
    [b]if[/b] [b][color=#0000ff]exists[/color][/b] [i][color=#0000ff]process[/color][/i] [color=#408000]myAppName[/color] [b]then[/b]
        [b]set[/b] [color=#408000]isRunning[/color] [b]to[/b] [color=#4a1e7f]true[/color]
    [b]end[/b] [b]if[/b]
[b]end[/b] [b]tell[/b]
[b]if[/b] [color=#408000]isRunning[/color] [b]is[/b] [color=#4a1e7f]true[/color] [b]then[/b]
    [b]tell[/b] [i][color=#0000ff]application[/color][/i] [color=#408000]myAppName[/color] [b]to[/b] [b][color=#0000ff]quit[/color][/b]
[b]else[/b]
    [b]tell[/b] [i][color=#0000ff]application[/color][/i] [color=#408000]myAppName[/color] [b]to[/b] [b][color=#0000ff]activate[/color][/b]
[b]end[/b] [b]if[/b]
 

wrignj08

macrumors newbie
Original poster
Jun 13, 2009
28
0
Ok so maybe it is not perfect, for example it does not appear to work for textedit. it will open the app but then the script will not quit it. suggestions?

Code:
set myAppName to "textedit"
set isRunning to false
tell application "System Events"
	if exists process myAppName then
		set isRunning to true
	end if
end tell
if isRunning is true then
	tell application myAppName to quit
else
	tell application myAppName to activate
end if
 
Last edited by a moderator:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.