1

I wrote the following script to select copied/pasted Finder items in the target folder I pasted them into.

tell application "Finder" set thePath to POSIX path of (insertion location as alias) set theFiles to paragraphs of (the clipboard) set theOutput to {} set theFile to {} repeat with i from 1 to count of theFiles set theFile to POSIX file (thePath & item i of theFiles) as text set end of theOutput to theFile as text end repeat reveal theOutput activate end tell --return theOutput 

It works great on most files and folders alike, even those that might have " somewhere in the name.

However, when the copied/pasted files are a symbolic link, Finder resolves them to the original files instead of selecting/revealing the link files (which is what I am aiming to achieve).

Is there some way to change the script to make it not follow/resolve link files to their target and reveal the link files themselves in Finder instead?

2
  • 1
    That's what reveal does — it is basically the applescript equivalent of the finder's show original (which I think even used to be called 'reveal'). Try using the select command. Also, while looking through your script, I don't really see the advantage of all the coercing that's being done. Commented Aug 1, 2023 at 22:14
  • @Mockman Thanks for the reply. I'm sure it could be a lot simpler. This is just what I came up with to resolve some hiccups I was seeing between selecting one item vs several. I'm far from an Applescript expert. but regarding what you wrote, i use an Automator service to create the symlinks via shell script, which also echoes & reveals/selects them in Finder afterwards - not their targets. So it seems possible, but I'm not sure how to do it beyond that point after copying/moving them elsewhere. Commented Aug 2, 2023 at 7:40

2 Answers 2

1

In the example below, the 'man rsync269.pdf' file was actually a symbolic link (created with ln -s). Note that the select command's result describes this file as an 'alias file' rather than as a 'document file'. The third item is now a folder.

Update: Added support for folders

tell application "Finder" set tPath to (insertion location) --> folder "random-storage" of folder "username" of folder "Users" of startup disk of application "Finder" set fileNameList to paragraphs of (the clipboard) --> {"man rsync269.pdf", "man unzip.pdf", "man pages"} set tOutput to {} repeat with ii from 1 to count of fileNameList try -- if file (includes alias files) set end of tOutput to file (item ii of fileNameList) of tPath on error -- if folder (technically, if not a file) set end of tOutput to folder (item ii of fileNameList) of tPath end try --> alias file man rsync269.pdf of folder "random-storage" of folder "username" of folder "Users" of startup disk end repeat select tOutput --> {alias file "man rsync269.pdf" of folder "random-storage" of folder "username" of folder "Users" of startup disk of application "Finder", document file "man unzip.pdf" of folder "random-storage" of folder "username" of folder "Users" of startup disk of application "Finder", folder "man pages" of folder "random-storage" of folder "username" of folder "Users" of startup disk of application "Finder"} end tell 

Alternatively, I think this would likely also work if you coerced everything to text and then prefaced each reference with file (i.e. composing a file url).

4
  • Eureka! The thing I was doing was all based on the (mis)understanding that I needed to convert the files to the text of the filenames and then prepend the filepath as a string to the name of each file and then reveal each file at the new location. This is so much simpler. Thank you! Commented Aug 3, 2023 at 4:14
  • Next step: Finding a way to compare the clipboard to what exists in a folder so that, if/when Finder makes a duplicate of a pasted file with a new name (ending with either " copy" or " 2", etc.), that I can skip the script to select the pasted files by name in those cases, and leave it to Finder to select the newly-created duplicate files. Commented Aug 3, 2023 at 4:21
  • I just realized this doesn't work if the copied items are folders! Is there some way it could be modified so it still works with folders too? Commented Aug 3, 2023 at 4:36
  • 1
    @MaxWell I've added a check to deal with folders. Regarding duplicates, I think that would be a separate problem. There would be myriad edge cases, for example… what if you had a " 2" and a " 4" already in the target folder. In any case, you wouldn't really be dealing with the clipboard anymore. Commented Aug 3, 2023 at 19:02
0

I've also added the following to the end of the script in the accepted answer in order to make the debug text for the paths of files that were copy/pasted and selected in Finder easier to read.

set debugtext to {} repeat with j from 1 to count of tOutput set end of debugtext to (POSIX path of ((item j of tOutput) as string)) & return end repeat return debugtext 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.