Jump to content

Workflow to duplicate a file


Recommended Posts

Hello, I am trying to replicate the way macOS duplicates a file but I'm very new to scripting and Alfred and what I'm doing isn't working too well.

Currently I have this extremely basic script, which I've commented out since they don't really work.

#cp $1 $1
#cp {query} {query}_copy2

The problem is I can't get the name of the duplicate to change so that it has something at the end of the name, such as "copy 1".

I am looking for a result similar to the image below. 

It would be great as an Alfred file browser action to duplicate the in the same location

1026346643_Screenshot_2020-11-0906_53.29_0KTVGN.png.98f74d6f8b8e6895e161d8ae06d03045.png

Link to comment

There are several problems with the code:

  • cp $1 $1 is trying to copy the file over itself.
  • cp {query} {query}_copy2 doesn’t take into account the file extension. The appended text would be after it, disassociating the file from the relevant apps. It would also not prevent overwriting existing files.
  • Neither line quotes the arguments, meaning it’s going to break on spaces and some special characters.

The way to go about it is to ask the Finder to do the duplication, because it knows what rules to use. There you go, this will do what you want.

Link to comment
  • 2 weeks later...

Oh my goodness, THANK YOU @vitor, Im sorry for replying so late, I was logged out and didn't realize I got any responses, but wow does the workflow you made do exactly what I was looking for so perfectly. And yeah the code I posted was practically pointless and was going nowhere, just thought I would make some attempts before asking for help. And "ask the Finder to do it, because it know what the rules are" is a very good way to put it and a nice way of thinking, will definitely try and jump into AppleScript as soon as I can.

Link to comment
27 minutes ago, Alfred_powerUser said:

but wow does the workflow you made do exactly what I was looking for so perfectly.

 

Glad to know!

 

27 minutes ago, Alfred_powerUser said:

And yeah the code I posted was practically pointless and was going nowhere, just thought I would make some attempts before asking for help.

 

That’s the correct though process. In general, people on the forum like to help people who help themselves. Trying to do it sends a very positive message and makes us more eager to help.

 

29 minutes ago, Alfred_powerUser said:

will definitely try and jump into AppleScript as soon as I can.

 

I recommend against that. Yes, it was the correct tool in this case, but AppleScript is a bad language. If it weren’t because it allows us to interact with (some) apps, almost no one would use it (which is to say, most of us use it because we have no choice, not because we like it).


In addition, Apple doesn’t care for it (or automation in general) anymore. More of their own apps lose AppleScript support with each macOS release, and if the company who controls the language doesn’t care for it, that’s a good sign you shouldn’t invest in it. Coupled with the fact that AppleScript is weird and what you learn in it doesn’t easily translate to other languages, learning it now is a waste.


Instead, learn any other popular scripting language, like Ruby or Python (or even Bash/ZSH). You’ll find considerably more resources to learn and solve any doubts, will be able to do more, and if you ever switch to another language your knowledge will still be applicable.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...