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.
------------------------------------------------------------------------------
# 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
------------------------------------------------------------------------------