Hello,
I’ve recently switched to OSX (on a G5) from OS9 where I used Applescript and OneClick extensively. Now I’m in the process of converting all my automation using UI Browser, Applescript and iKey. In doing this conversion, my brain locked where I just can’t seem to be able to get just the file name from a selected file in an active window and place it in a variable. Here is my script so far.
tell application “Finder”
activate
set file_name to selection as string --returns the path of the selected file in the active window
display dialog file_name
end tell
This script returns the path of the file but I want just the file name. I tried all kinds of variations on ‘set’ or ‘get’ or whatever and no go.
I remember having this problem in the past. The script below showed both the file name only and the path in OS9. ‘Copy the selection’ doesn’t seem to work in OSX. What am I missing here? Keith.
tell application “Finder”
activate
copy the selection
set the_file to the clipboard
set the_path to selection
display dialog (the_file as text)
display dialog the_path as text
end tell
Replying to a very old post here, but I’m curious about how to handle a multiple file selection as advised in the last post in the thread.
I found this script, which copies the full path of the selected file(s) to the clipboard:
set theSel to (selection of application "Finder")
set pathList to {}
repeat with anItem in theSel
set the end of pathList to (anItem as text)
end repeat
set savedDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to "
"
set the clipboard to pathList as string
set AppleScript's text item delimiters to savedDelimiters
And I’ve tried to edit it to only return the file names without the paths, as I’d like to select a a bunch of newly created files on the desktop and be able to paste the file names into a document, as part of a work flow.
But I can’t get past a bunch of errors when I try to add the “file specification” to only return the file name, not the complete path.
try this, but in almost all cases the clipboard detour is not needed
set theSel to (selection of application "Finder")
set pathList to {}
repeat with anItem in theSel
set the end of pathList to name of (info for anItem as alias)
end repeat
set {TID, text item delimiters} to {text item delimiters, return}
set the clipboard to pathList as text
set text item delimiters to TID