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.

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

3 Answers 3

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
6
  • What OS X are you using. Because that does not work on my Mavricks
    – markhunte
    Commented Sep 13, 2014 at 20:20
  • I am using Snow Leopard Commented Sep 13, 2014 at 21:39
  • It Looks like the ppid is retuning the root pid id of 1 in 10.9
    – markhunte
    Commented Sep 14, 2014 at 0:38
  • @markhunte: Yes correct, on Mavricks its not working. Did you find any way to make it work?
    – batman
    Commented Sep 17, 2014 at 4:26
  • No. I did try but could not get anything to work..
    – markhunte
    Commented Sep 17, 2014 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

2
  • When you run this from a service it will return "Automator runner" not the app that you selected the service from.
    – markhunte
    Commented Sep 12, 2014 at 22:28
  • @markhunte Well I don't know how I missed that. Fixed.
    – 0942v8653
    Commented Sep 12, 2014 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 .