Friday, October 22, 2021

#1 2021-09-25 08:30:26 am

jrgarrigues
Member
Registered: 2020-05-22
Posts: 2

Functionality of AppleScript's "Duplicate" command

I've got a very simple AppleScript that I use as a service in Finder:

Applescript:


on run {input}
   try
       tell application "Finder" to duplicate input to "/Volumes/named_folder" with replacing
   end try
end run

Using this as a service allows me to right-click on a file in Finder and execute the script on a file or folder. It's important that this script "duplicates" the file/folder in question - i.e., leaves the original where it is, and places a "copy" of the file in the newly selected location. The script has served me well for several years.

Sometime over the course of the past few months, the functionality of the "duplicate" command seems to have changed. Now my script "moves" the selected file from the original location to the new location - it doesn't leave a copy of the file in the original location. I can't find anything online that suggests a change to the "duplicate" command. The dictionary entries for "duplicate" and "move" still appear to be the same as they were before.

I'm looking for a way to modify my AppleScript so that it will leave the original version in place.

Thanks for any suggestions you may have,
John

Model: MacBook Pro 16"
AppleScript: 2.7
Browser: Safari 537.36
Operating System: Other


Filed under: command, copy, Move, duplicate

Offline

 

#2 2021-09-25 11:44:17 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2092

Re: Functionality of AppleScript's "Duplicate" command

I think (not sure), the problem is you duplicate input of Automator, which is list of AppleScript aliases. And, the duplicate command of Finder for multiple items is intended for duplicating Finder selection. Logically, to duplicate Automator input you should use repeat loop (duplicate one by one).

Applescript:


on run {input}
   repeat with anAlias in input
       try
           tell application "Finder" to duplicate (contents of anAlias) to "/Volumes/named_folder" with replacing
       end try
   end repeat
end run

Making AppleScript alias list Finder alias list should help as well (not tested):

Applescript:


on run {input}
-- convert to Finder alias list
   tell application "Finder" to set input to input as alias list
   try
       tell application "Finder" to duplicate input to "/Volumes/named_folder" with replacing
   end try
end run

Maybe, the main problem is you try to duplicate Automator input object (reference to this Automator list) instead of its contents (not tested as well):

Applescript:


on run {input}
   try
       tell application "Finder" to duplicate (contents of input) to "/Volumes/named_folder" with replacing
   end try
end run

Last edited by KniazidisR (2021-09-25 12:20:16 pm)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Online

 

#3 2021-09-25 04:45:51 pm

jrgarrigues
Member
Registered: 2020-05-22
Posts: 2

Re: Functionality of AppleScript's "Duplicate" command

@KniazidisR - thank you for your reply. A strange thing happened; when I went to try out your suggestions, I did something to my Automator file that made the original script work again. It must have been "hung up" or something - now it's working fine.

Most of the time, I have to hunt for the bugs. Sometimes they leave all by themselves - though now I'm really curious about what caused this problem, this time I'm just going to let it be! hmm

Thanks again,
John

Offline

 

#4 2021-09-25 11:41:24 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2092

Re: Functionality of AppleScript's "Duplicate" command

It’s hard for me to say right now if you’ll get strange errors again with your approach. I myself always get the contents of the input into a list variable first, and then work with that list inside tell blocks of applications.

Last edited by KniazidisR (2021-09-25 11:43:40 pm)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Online

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)