• 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: path of a Finder item
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: path of a Finder item


  • Subject: Re: path of a Finder item
  • From: Christopher Stone <email@hidden>
  • Date: Wed, 22 Mar 2017 19:16:46 -0500

On Mar 22, 2017, at 07:09, Jean-Christophe Helary <email@hidden> wrote:
So basically I got the thing right from the start *except* for that right ? :-) I am *so* getting better at AS everyday...


Hey Jean-Christophe,

It's a good feeling when that starts to happen.  :)

The thing is just a way to call an app from the command line to act on a folder that I select manually (instead of copying the folder, turning to Terminal, typing the command, space, pasting the path, hitting return...) So I guess I would know if I selected more than one item, but thank you for the reminder that one always need to check for all the cases for error handling.

Eventually you'll make a mistake, so it's a good idea to manage that in advance – particularly when there's not a lot of effort involved.  It builds good habits.

Here are a few toys for you to play with:

------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2011/10/16 02:11
# dMod: 2014/10/15 10:56
# Appl: Terminal
# Task: CD to Front Finder Window's Directory.
# Libs: None
# Osax: None 
# Tags: @Applescript, @Terminal, @CD, @Finder, @Insertion_Location
------------------------------------------------------------------------------

# Try-block elided due to FastScripts' better error dialog.

tell application "Finder" to set _dir to insertion location as alias

tell application "Terminal"
    tell selected tab of front window
        if its busy = false then
            do script "cd " & (quoted form of (POSIX path of _dir)) in it
        else
            do script "cd " & (quoted form of (POSIX path of _dir))
        end if
    end tell
end tell

------------------------------------------------------------------------------

This one's been polished over about 14 years or so.

------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2010/10/06 06:43
# dMod: 2015/04/27 18:38
# 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

    

    tell application "Finder"

        

        set finderSelectionList to selection as alias list

        

        if length of finderSelectionList = 0 then
            set finderSelectionList to insertion location as alias 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 AppleScript's text item delimiters to linefeed
        set finderSelectionList to finderSelectionList as string

        

        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 one is leverages the code of the previous script – adds quoting – and transforms POSIX_Paths to $HOME-POSIX_Paths where possible.

I wrote the quoting and $HOME-basing code today, so it's not been well-tested.

------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2015/04/27 18:38
# dMod: 2017/03/22 19:05
# Appl: Finder
# Task: Get quoted $HOME-base 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, @Quoted
------------------------------------------------------------------------------
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
------------------------------------------------------------------------------

try

    

    set homeFolderPath to POSIX path of (path to home folder as text)

    

    tell application "Finder"

        

        set finderSelectionList to selection as alias list

        

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

        

    end tell

    

    if length of finderSelectionList > 0 then

        

        repeat with theAlias in finderSelectionList
            set theAlias's contents to quoted form of (POSIX path of theAlias)
        end repeat

        

        set AppleScript's text item delimiters to linefeed
        set finderSelectionList to finderSelectionList as string

        

        set regEx to "'" & (homeFolderPath & "(.+)")
        set finderSelectionList to its cngStr:regEx intoString:"~/'$1" inString:finderSelectionList

        

        set the clipboard to finderSelectionList

        

    end if

    

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

------------------------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------------------------
on cngStr:findString intoString:replaceString inString:dataString
    set anNSString to current application's NSString's stringWithString:dataString
    set dataString to (anNSString's ¬
        stringByReplacingOccurrencesOfString:findString withString:replaceString ¬
            options:(current application's NSRegularExpressionSearch) range:{0, length of dataString}) as text
end cngStr:intoString:inString:
------------------------------------------------------------------------------
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
------------------------------------------------------------------------------

--
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: path of a Finder item
      • From: Jean-Christophe Helary <email@hidden>
References: 
 >path of a Finder item (From: Jean-Christophe Helary <email@hidden>)
 >Re: path of a Finder item (From: Shane Stanley <email@hidden>)
 >Re: path of a Finder item (From: Jean-Christophe Helary <email@hidden>)

  • Prev by Date: Re: path of a Finder item
  • Next by Date: Re: path of a Finder item
  • Previous by thread: Re: path of a Finder item
  • Next by thread: Re: path of a Finder item
  • Index(es):
    • Date
    • Thread