Reputation: 4396
I want to get the filepath of the front file in the Finder, such as /Users/user/Downloads/file.png
and I am very confused with the way macOS
and AppleScript handle file paths, POSIX with slashes and natively with colons. I tried both of these:
tell application "Finder"
set temp to selection
repeat with i from 1 to length of temp
set the_item to item i of temp
set item_posix to the_item as POSIX file
set the_result to POSIX path of item_posix -- returns only the file's name and extension
return get the path of the_item -- returns an error
end repeat
end tell
I once succeeded and it was so convoluted with as file
and as alias
that I can't remember how it worked.
How can I get the filepath of the front file in the Finder?
Update: I'm interested in a single path for the sake of the syntax and I can handle multiple paths with a loop.
Upvotes: 2
Views: 6897
Reputation: 285059
In Finder the selection
property returns always a list of Finder file specifiers or an empty list.
The easiest way using your syntax is to coerce the selection to alias list
and get the POSIX path from an alias
tell application "Finder"
set temp to selection as alias list
repeat with i from 1 to length of temp
set the_item to item i of temp
set the_result to POSIX path of the_item
return the_result
end repeat
end tell
POSIX file
is only needed for the opposite, to get an HFS path or alias from a POSIX path.
If you want only the first item of the selection
you don't need a loop but you have to check for empty list
tell application "Finder"
set temp to selection as alias list
if temp is not {} then
return POSIX path of item 1
end if
end tell
Upvotes: 3
Reputation: 3142
I find this following, alternate AppleScript solution, as the choice I would use.
While Finder.app is front most, using the keyboard shortcut… option + command + c will copy the Posix Paths of the selected Finder items to the clipboard.
tell application "System Events" to keystroke "c" using {option down, command down}
Here is a little utility AppleScript I wrote which incorporates the action of retrieving the Posix Paths of the selected Finder items… if Finder is front most and there are selected items.
Ultimately, if Finder is not front most, if the selected text in the current document you are viewing is a Posix Path or HFS Path , it will reveal that item in Finder for you.
-- Add A Delay For Testing Purposes.
-— Allowing You Time To Bring Application "Finder" Or Other Document To The Front
-— While Running This Code In Your Script Editing Application
delay 10
tell application "Finder" to set finderIsFrontmost to frontmost
if finderIsFrontmost then
-- Copy Selected Files In Finder, As Path Names
tell application "System Events" to keystroke "c" using {option down, command down}
else
-- Copy Selected File Path Text From Current Doc In Frontmost App
tell application "System Events" to keystroke "c" using {command down}
delay 0.1
-- The "-R" Reveals Copied File Path In Finder, Rather Than Opening If It's A Path To A File
try
do shell script "open -R " & quoted form of (the clipboard)
delay 0.1
tell application "Finder" to activate
repeat until application "Finder" is frontmost
delay 0.2
end repeat
delay 0.2
tell application "System Events" to key code 124 using {command down, option down, control down}
on error errMsg number errNum
try
do shell script "open -R " & quoted form of POSIX path of (the clipboard)
delay 0.1
tell application "Finder" to activate
repeat until application "Finder" is frontmost
delay 0.2
end repeat
delay 0.2
tell application "System Events" to key code 124 using {command down, option down, control down}
on error errMsg number errNum
activate
display alert errMsg message ¬
"Either The File or Folder No Longer Exists" & linefeed & linefeed & "OR" & linefeed & linefeed & ¬
"The Selected Text Contains Starting or Trailing White Spaces" & linefeed & linefeed & ¬
"Please Make Sure Your Selected Text Has No Empty Spaces At The Beginning or End Of Your Selection" as critical buttons {"OK"} giving up after 20
end try
end try
end if
Upvotes: 0
Reputation: 7555
With the inuxmint-20-cinnamon-64bit.iso files selected in the Downloads folders in Finder, here are some examples:
This command:
tell application "Finder" to get selection
Result:
--> {document file "linuxmint-20-cinnamon-64bit.iso" of folder "Downloads" of folder "me" of folder "Users" of startup disk}
This command:
tell application "Finder" to get selection as alias
Result:
-> {alias "Macintosh HD:Users:me:Downloads:linuxmint-20-cinnamon-64bit.iso"}
This command:
tell application "Finder" to set aliasItemName to selection as alias
Result:
--> {alias "Macintosh HD:Users:me:Downloads:linuxmint-20-cinnamon-64bit.iso"}
These commands:
set posixPathName to POSIX path of aliasItemName
return posixPathName
Result:
"/Users/me/Downloads/linuxmint-20-cinnamon-64bit.iso"
Note that set posixPathName to POSIX path of aliasItemName
is done outside of the context of Finder as it does not understand POSIX path
as it's a part of Standard Additions, not Finder.
Upvotes: 2