• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Copying the path of files selected in Finder
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Copying the path of files selected in Finder


  • Subject: Re: Copying the path of files selected in Finder
  • From: Christopher Stone <email@hidden>
  • Date: Sat, 29 Apr 2017 17:40:59 -0500

On 04/29/2017, at 04:26, Jean-Christophe Helary <email@hidden> wrote:
tell application "Finder"
set _selectedItem to selection


Hey Jean-Christophe,

Rule-of-the-thumb — never get the Finder-selection without adding as alias list.

(Unless you very specifically want Finder-references or text.)

The as alias list form is faster, and aliases are portable while Finder-references are not. 

Test this for yourself by selecting 20-30 (or more) items in the Finder an alternately running these scripts:

------------------------------------------------------------------------------
tell application "Finder"
set finderSelectionList to the selection
end tell
------------------------------------------------------------------------------
tell application "Finder"
set finderSelectionList to the selection as alias list
end tell
------------------------------------------------------------------------------





This script has been refined many times for efficiency over 15 years of use.

Since I want path-strings instead of file-objects I'm using selection as text, instead of selection as alias list.  The coercion is faster – even with the conversion to paragraphs.

------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2010/10/06 06:43
# dMod: 2017/04/29 17:32
# Appl: Finder
# Task: Get posix path of selected item(s) or if none the front window and copy to clipboard.
# Libs: ELb
# Osax: None 
# Tags: @Applescript, @Finder, @Posix, @Path, @Selection, @Clipboard
------------------------------------------------------------------------------

try

    

    set AppleScript's text item delimiters to linefeed

    

    tell application "Finder"

        

        set finderSelectionList to paragraphs of (selection as text)

        

        if length of finderSelectionList = 0 then
            set finderSelectionList to insertion location as text as list
        end if

        

    end tell

    

    if length of finderSelectionList > 0 then

        

        repeat with theAlias in finderSelectionList
            set theAlias's contents to POSIX path of theAlias
        end repeat

        

        set finderSelectionList to finderSelectionList as text

        

        set the clipboard to finderSelectionList

        

    end if

    

on error e number n
    stdErr(e, n, true, true) of me
end try

------------------------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------------------------
on stdErr(e, n, beepFlag, ddFlag)
    set e to e & return & return & "Num: " & n
    if beepFlag = true then
        beep
    end if
    if ddFlag = true then
        tell me
            set dDlg to display dialog e with title "ERROR!" buttons {"Cancel", "Copy", "OK"} default button "OK"
        end tell
        if button returned of dDlg = "Copy" then set the clipboard to e
    else
        return e
    end if
end stdErr
------------------------------------------------------------------------------

This script takes 0.22 seconds to put the POSIX Paths of 100 files on the clipboard (on my system).

By contrast yours takes 1.65 seconds.

I'm using FastScripts to run it with a dedicated keyboard shortcut in the Finder (Cmd-Shift-P).

--
Take Care,
Chris

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Copying the path of files selected in Finder
      • From: Emmanuel Lévy <email@hidden>
References: 
 >Copying the path of files selected in Finder (From: Jean-Christophe Helary <email@hidden>)

  • Prev by Date: display as text anomaly
  • Next by Date: Re: Copying the path of files selected in Finder
  • Previous by thread: Re: Copying the path of files selected in Finder
  • Next by thread: Re: Copying the path of files selected in Finder
  • Index(es):
    • Date
    • Thread