1

Is there a way to determine which application launched a Automator Service?

The idea is to check the app that launched the service, with a shell/apple script and react differently depending which app it was.

EDIT:

So when I do something like this...

Launching service

the service, created in Automator, can tell which application was that it was launched from.

EDIT2:

The solution suggested, to query for the front most app using Applescript, while a possible solution is far from ideal. It seems to me that if the system is busy or there are delays in the launching of the workflow, it could return the wrong app and confuse the workflow.

  • workflow service ? – Ruskes Sep 12 '14 at 21:37
  • Sorry, yes an automator workflow as service. see my edit. – Vic Sep 12 '14 at 21:44
  • try this in terminal sudo opensnoop -n WorkflowService – Ruskes Sep 13 '14 at 0:27
2

Buscar's comment made me check the process hierarchy and it turns out, a service is a sub-sub-sub process of the application that launched it.

Application and it's service

So, even though in many cases, a query for the frontmost application may be ok, I think looking at the process tree is better.

Here is some sample code from a test Service Workflow that for me appears to work every time...

on run {input, parameters}

    set arpid to (do shell script "echo $PPID")
    display dialog "Automator Runner PID is: " & arpid

    set srpid to (do shell script "ps -o ppid -p " & arpid & " | tail -1")
    display dialog "Service Runner PID is: " & srpid

    set appid to (do shell script "ps -o ppid -p " & srpid & " | tail -1")
    display dialog "Application PID is: '" & first word of appid & "'"

    tell application "System Events"
        set activeApp to (name of first process whose unix id is (appid as integer))
    end tell

    display dialog activeApp

    return input
end run
  • What OS X are you using. Because that does not work on my Mavricks – markhunte Sep 13 '14 at 20:20
  • I am using Snow Leopard – Vic Sep 13 '14 at 21:39
  • It Looks like the ppid is retuning the root pid id of 1 in 10.9 – markhunte Sep 14 '14 at 0:38
  • @markhunte: Yes correct, on Mavricks its not working. Did you find any way to make it work? – batman Sep 17 '14 at 4:26
  • No. I did try but could not get anything to work.. – markhunte Sep 17 '14 at 8:03
1

You can place this Applescript in your workflow:

tell application "System Events" to get name of first process whose frontmost is true

It will output the name of the current foreground app (you can't really call a service from an inactive application). You can then pass it as a string into another action like this:

Workflow with the applescript passed into a textedit document

  • When you run this from a service it will return "Automator runner" not the app that you selected the service from. – markhunte Sep 12 '14 at 22:28
  • @markhunte Well I don't know how I missed that. Fixed. – 0942v8653 Sep 12 '14 at 23:15
1

AppleScript:

tell application "System Events"    
    -- Get App Name
    set frontApp to (path to frontmost application as Unicode text) 
    -- Match App Name
    if frontApp ends with "ThisParticular.app:" then
        -- Do this and that
    end if

end tell

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .