Benches macOS

14

apple script service does not work correctly

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

I want to make a service in snow leopard that when activated toggles another application. if the other application is running then calling this service should quit it. if the other application is not running, calling this service should start it. let's say the other application is safari. It does not seem to matter which application it is. the service I'm making consist of the following single action

<pre style="
font-family: Monaco, 'Courier New', Courier, monospace;
font-size: 10px;
margin: 0px;
padding: 5px;
border: 1px solid #000000;
width: 720px;
color: #000000;
background-color: #ADD8E6;
overflow: auto;"
title="this text can be pasted into the Script Editor">
on run {input, parameters}

tell application "System Events"
set x to (exists process "Safari")
if x then
tell application "Safari" to quit
else
tell application "Safari" to launch

end if

end tell

end run
</pre>

it works fine when run from automator but it does not work correctly when called as a service. if safari is running then calling the service quits it. but if safari is not running then calling the service launches safari for a brief moment and then safari quits. why?

14 replies

Sign in or join Beachball Help to reply.

  1. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Just remove {input, parameters} from the first line of your script.

  2. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Pierre L. wrote:
    Just remove {input, parameters} from the first line of your script.

    that changes nothing.

  3. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Try using:

    on run {input, parameters}
    tell application "System Events"
    if (exists process "Safari") then
    tell application "Safari" to quit
    else
    tell application "Safari" to launch
    return
    end if
    end tell
    end run

    (48981)

  4. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    On my computer, both your script and V.K.'s one return error number -1721 when they are launched from the AppleScript Editor, whereas both of them work flawlessly after I remove the {input, parameters}.

  5. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    let me clarify. as i said, I'm trying to make a service using automator. therefore I need to do it from automator using 'run applescript' action. the correct syntax there is to enclose everything into


    on run {input, parameters}

    end run

    of course you don't need those things in AppleScript Editor but I must use automator.

  6. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    thanks Niel,
    I should have mentioned that I tried that variation too before posting. the effect is the same. it works perfectly when run from automator but produces the same strange result when called as a service.

  7. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    I have played a little with Automator, about which I don't know much.

    I can't tell you why the service you are trying to implement doesn't work properly. However I can suggest a workaround that seems to work very well, at least on my computer.

    First save the following script anywhere, for example on your desktop under the name “LaunchOrQuit.scpt”:

    tell application "System Events"
    if (exists process "Safari") then
    tell application "Safari" to quit
    else
    tell application "Safari" to launch
    end if
    end tell

    Then use the following code in Automator:

    on run
    tell (load script POSIX file "/Users/pierre/Desktop/LaunchOrQuit.scpt") to run
    end run

    (“pierre” is the name of my home folder.)

  8. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    ok, thanks. this does work although it requires creating yet another file. but what I would really like to know is why the original script does not work as expected. it's too simple to produce any errors so what the Zarquon is happening here? I would have to suspect a bug. unless someone can explain why it would do this?

  9. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    well, first, break up the nested Tell blocks: snow leopard tightened up rules about how objects get called from within Tells

    tell application "System Events" to set x to (exists process "Safari")
    if x then
    tell application "Safari" to quit
    else
    tell application "Safari" to launch
    end if

    the only reason I can think of why Safari would launch and then immediately quit, though, is if the service is (for some reason) getting called twice. I don't have SL to test it on, though (that's a christmas present to myself I haven't opened yet), so that's just speculation.

  10. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    thanks, but that does not work either. and it doesn't mind the nested tells in the separate script as suggested by Pierre or when I run it directly from automator. this only happens when it's called as a service. I also don't think the service is called twice. it works just as it should using the workaround suggested by Pierre. and as I mentioned, if safari is already running, then calling the service just quits safari. it does not quit and then restart safari.

    Message was edited by: V.K.

  11. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Hello

    If Pierre L's method works, the following scripts should work as well.

    And if they do, there should be something strange happening in the original code, which I don't understand. I'd consider it fatal bug in AppleScript in service, for it makes even a script of this simplicity fail to serve reliably.

    If they don't work either, I'd suspect the problem cause would lie in the compiler of Automator (AppleScript) service, for the difference between these scripts and Pierre L's is how the callee script (object) is compiled.

    Just my thought,
    H


    --CODE1
    on run {input, parameters}
    tell o to run
    end run

    script o
    tell application "System Events"
    if (exists process "Safari") then
    tell application "Safari" to quit
    else
    tell application "Safari" to launch
    end if
    end tell
    end script
    --END OF CODE1

    --CODE2
    on run {input, parameters}
    tell mk() to run
    end run

    on mk()
    script o
    tell application "System Events"
    if (exists process "Safari") then
    tell application "Safari" to quit
    else
    tell application "Safari" to launch
    end if
    end tell
    end script
    end mk
    --END OF CODE2

  12. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    I'm sorry, I think I misunderstood the goal here. you want the app to either (a) launch, or (b) quit and relaunch? in that case, try:

    tell application "System Events"
    if exists process "Safari" then
    tell application "Safari"
    quit
    end tell
    repeat 120 times
    if not (process "Safari" exists) then
    exit repeat
    end if
    delay 0.5
    end repeat
    end if
    end tell
    tell application "Safari" to launch

  13. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Hi Hiroto.
    Both of your scripts behave exactly the same way as my original one.
    the only thing that works correctly is Pierre's workaround with creating a completely separate script.
    I also think it's a bug.

  14. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    twtwtw wrote:
    I'm sorry, I think I misunderstood the goal here. you want the app to either (a) launch, or (b) quit and relaunch?

    no, that's not what I want. you understood it correctly before. I want it it to a) launch safari if safari not running and b) quit safari if safari is running.
    I was just responding to your idea that maybe somehow the service is being called twice which . I know that's not the case because it works as it should in situation b) but not in a).