Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I'm trying to build a Service that does the following:

  • If some folder/files are selected in Finder, copy (all) the paths to the clipboard
  • If nothing is selected, copy the "Path" of the window.

Digging around and getting inspiration from posts such as Copying the current directory's path to the clipboard and MacYourself's Copy file or folder path to the clipboard in Mac OS X Lion I've got to the point where I can get the paths of the selection, but I seem to be stuck when trying to get the path of the current window if selection empty.

UPDATE 13-Dec-2013. I've got some useful feedback (see http://apple.stackexchange.com/a/113612/7488; thanks @Flavin) so I have updated the code to the one below:

    on run {input, parameters}

        set l to {}
        tell application "Finder"
            set sel to (get selection)
            if not sel = {} then -- there are some file/folders selected

                repeat with f in (get selection)
                    set end of l to POSIX path of (f as alias)
                end repeat

            else --no stuff is selected, get the current location path

                set end of l to POSIX path of (insertion location as alias)

            end if
        end tell
        set text item delimiters to linefeed
        set the clipboard to (l as text)

    end run

The logic seems solid, and it works as long as I have "something" selected in Finder.

I suspect that the Service might not be "Active" when there is nothing selected, (that is, when the desired result would be to copy the current path to the clipboard).

In this situation, the Finder window appears thusly:

Finder With nothing selected

But trying to active the Service shows an "empty" service list:

Finder No services apply

When I have something selected, the Services list is populated:

Services Available when some File is selected

The "Copy File Path" that I've defined is associated with "Files and Folders" in the Services Preferences -- what might be the issue?

share|improve this question

2 Answers 2

I think = is what you're looking for.

set sel to (get selection)
if not sel = {} then
    --stuff is selected
else
    --no stuff is selected
end if

Or remove the not and flip the cases

set sel to (get selection)
if sel = {} then
    --no stuff is selected
else
    --stuff is selected
end if
share|improve this answer
    
Thanks for the confirmation; the logic seems solid but the behavior is still not what I need. I'll be updating the question with some info that might be relevant –  JJarava Dec 13 '13 at 22:08
up vote 1 down vote accepted

It now works. The key is on how the "Automator" service is defined.

When initially configured, the Automator Service was defined as "Files or Folder" in "Finder":

Service for Input Files and Folders in Finder

So in the Preferences the Service is associated with Files and Folders:

Preferences-File-Folders

And the result is that the service is not "active" when there is no selection (we get the same image than in the question):

Finder No services apply

Changing the definition of the service to "No Input":

Service with No Input

Means that the Service is available when there is no selection in Finder:

Service associated with Finder and no input

Checking in the Services Preferences, now the Service is associated with the "General" category:

Service in the General Category

And the behavior is as expected.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.