Applescript activation switch?

Discussion in 'Mac Programming' started by wrignj08, Jun 13, 2011.

  1. macrumors newbie

    Joined:
    Jun 13, 2009
    #1
    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.
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    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]
    
     
  3. thread starter macrumors newbie

    Joined:
    Jun 13, 2009
  4. wrignj08, Jun 21, 2011
    Last edited by a moderator: Jun 21, 2011

    thread starter macrumors newbie

    Joined:
    Jun 13, 2009
    #4
    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
     
  5. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #5
    It's case sensitive. Use "TextEdit" instead of "textedit".
     
  6. thread starter macrumors newbie

    Joined:
    Jun 13, 2009

Share This Page