[AppleScript] Duplicate files with timestamp

Mikuro

Crotchety UI Nitpicker
Here's a script I just wrote today. It duplicates the currently selected files in the Finder, and then puts a timestamp in the name of the duplicates (instead of the generic "copy"). The timestamp uses a long date format that is properly sortable as plain text in any program: YYYY-MM-DD hh-mm-ss

For example, if you load the script with "MyFile.txt" selected at 2:15 PM, the duplicate will be named something like "MyFile 2008-03-16 14-15-01.txt".

It works with any number of files and folders selected at once.
 

Attachments

  • Duplicate selected files with timestamp.applescript.zip
    1.1 KB · Views: 219
Last edited:
Hi Mikuro

Could be just what I am looking for, but as a script newbie before I do something horribly wrong can you describe how:
a) to install / use it (applescript editor compile. Run each time one wants to rename a file?) and
b) it works from a user point of view.

Ideally I would love to be able to right-click a file (ctrl-click) and have a 'timestamp duplicate' option.
 
Thanks Mikuro - tried it out and works fine. Your script duplicates files and timestamps just as you say.

Only thing - it opens in the script editor and has to be manually run. I don't seem to be able to compile it to an .app so that it just runs when selected fom the scripts menu. I tried opening the .applescript file and doing a compile but that does not work, and I tried opening it in Textmate first and then pasting to the script editor. This approach generates an error on a compile. "Syntax Error - Expected expression but found unknown token". The character it is unhappy with is Â
 
I see. I didn't realize the script menu wouldn't work with plain-text scripts (.applescript files). Do this to convert it:

- Open the file in Script editor.
- Choose File > Save As
- Select "Script" as the File Format and save it.

That will give you a .scpt file that should work.

The reason I posted a .applescript file instead of a .scpt is that .scpts don't seem to work on different computers sometimes.

Or try downloading this .scpt I made. Maybe it'll work.
 

Attachments

  • Duplicate selected files with timestamp.scpt.zip
    4.5 KB · Views: 41
Thanks Mikuro, works fine now, and as I said above just what I have been looking for! - I should probably have figured that one out myself! Funny how when 'somebody available knows better' ones own thinking abilities don't wish to be stretched.
 
Hi, I downloaded a copy of your duplicator script today. It works very well with highlighted files.

As a new user to script editor (I am very asinine with computers), I have not been able to understand very much of the jargon presented in the tutorials. How do I create a script that duplicates itself a number of times? I want to prank a friend of mine- I'm not trying to create a virus.
 
You can get the location of the script file with "the path to me". Then you just need to tell the Finder to duplicate it.
Code:
tell application "Finder"
	repeat with i from 1 to 100
		duplicate (the path to me)
		delay 1
	end repeat
end tell
That'll make 100 copies, pausing for 1 second between each copy.

(Note: If you run this in Script Editor, it might duplicate the Script Editor application. Save your file as an application and run it from the Finder for best results.)
 
awesome

thank you so much!
I'm just curious, but how would I configure the file to duplicate itself to other folders?
 
Actually, instead of bugging you repeatedly for this information, do you know where I can find a guide to the script language in layman's terms?
Thanks!
 
Most scriptable applications contain some built-in documentation of their scripting features. In Script Editor, go to File > Open Dictionary, and then choose the application you want to script. In this case it would be the Finder, which is located at /System/Library/CoreServices/Finder.app

On a related note, I wrote an AppleScript to make opening the Finder's scripting dictionary (which is something frequently need) easier:
Code:
tell application "Script Editor"
	activate
	open POSIX file "/System/Library/CoreServices/Finder.app"
end tell

In the Finder's dictionary, you'll see the duplicate command, which has some optional parameters, including the to parameter. So "duplicate (the path to me) to the desktop" would work, for example.

Apple's AppleScript Language Guide is useful, but it's not a tutorial or a quick start guide. It's really about the more arcane aspects of AppleScript. Your best bet is probably to look through the scripting dictionaries of the apps you're interested it and Google around for examples and tutorials when you get stuck.
 
Great!
Thanks for the shortcut script.
I'll be looking into that

In the meantime, I was wondering if it is possible to embed photos, icons, etc. into the application for exporting via email? I know that this might take some java programming as well, but is it possible to bundle some photos with the app so that the script can access it on a different computer?

Thanks
 
Sure, put all the images in a folder, along with the script, then send the whole folder out as a package. Zip it up to safe some bandwidth.

If you want to create a single, double-clickable application that has all the resources include (images, icons, etc.), then I believe you're looking at creating a native Mac OS X application with XCode. I do not believe AppleScript has this capability, though I'm not experienced enough with it to say definitely one way or another.
 
Hi,
Thanks for the tip! I guess that makes sense, since scripts are designed to work with files and applications already present on the computer.

Thanks to Miruko, My friends and I did some experimenting with the code provided earlier that duplicates the application some number of times when opened. However, we found that it wasn't annoying enough, since it only occupies one process for every open copy of itself. Therefore, we figured that if we somehow made the program so that it could select and open the copies of itself ONLY after a given amount of time, it can lag up more disk space (again, this is just for fun, not for malicious purposes).

I figured that the modification should involve adding a timeout clause and repeat clause to make the application select after a certain number of copies is made, e.g. 5.

For example, this is what I want to happen after the script runs

click on app.
app duplicates itself 5 times
app selects itself and all 5 copies
app opens all selected programs
repeats

Thanks for helping me- I'm really a computer noob D:

tell application "Finder"
repeat with i from 1 to 100
duplicate (the path to me)
delay 1
end repeat
end tell
 
Hi, I have spent the last week doing some research on my script: I think it works pretty well:
What it does is it enables GUI modifying, dims the screen, mutes the audio, and then proceeds to duplicate itself.

As for the duplicating process, this is what I have.

tell application "Finder"

tell application "Finder"

repeat with i from 1 to 5
repeat with i from 1 to 5
duplicate (the path to me)
end repeat
*(code for selecting the copies of itself need to go here)
open the selection
end repeat
end tell

I'm not sure how to specifically target those new files.

Once again, sorry for my incompetence.
 
The duplicate command returns a reference to the new copy. So you just need to store it somewhere.

Try this:
Code:
tell application "Finder"
	set file_list to {the path to me}
	repeat with i from 1 to 5
		set file_list to file_list & {(duplicate file_list)}
	end repeat
end tell

Curly braces are how you make with lists in AppleScript. So first you make a list with one object, then you duplicate that list and append to it. Repeat as desired.

The result is 32 copies of the script, since it doubles the number 5 times over (2^5=32).
 
Thanks for the replication code!

Yesterday I tried to alter an example script that replaces the text in names on my desktop. I want to be able to set it so that the script automatically sets the search parameter as both file names and folders instead of a button prompt, but I am not sure how to notate this. Basically, I am trying to strip the script of its excess error messages and user choice.

Here is the code that I am trying to modify...

try
tell application "Finder" to set the source_folder to path to desktop folder as alias
end try
display dialog "Search and replace in:" buttons {"File Names", "Folder Names", "Both"} default button 3
set the search_parameter to the button returned of the result
 
I was sort of able to figure it out in that I could do

set the search_parameter to the "File Names"
or

set the search_parameter to the "Folder Names"

but I can't figure out how to do both.

Also How do I get a script to click a link on a webpage? I want to make a script application that clicks a login button on the safari web browser, can somebody show me an example of how this is done?

thanks!
 
It looks like the script goes on to test search_parameter. My guess is you should say set the search_parameter to "Both". But there's probably a simpler way to do this by editing the later part of the script so that it just does what you want and doesn't bother testing search_parameter (assuming you're not using the option to search only folder or only file names).

As for the Safari question, this is more complicated. The only way I know of doing it is to tell Safari to execute a bit of JavaScript. So the first thing to do is to figure out how to push that button with JavaScript, and then send that JavaScript snippet to Safari via AppleScript. I have a simple example of this somewhere, let me see if I can find it....

Here we go:
Code:
tell application "Safari"
	do JavaScript "
	function x() {
	return document.title
	}
	x();
	" in tab 1 of window 1
end tell
That will pull the title of the active web page into your script.

I think there's a submit() method of either buttons or forms (or both) that will do what you need, but the trickier part is finding the right form button through the script. I'm not an expert with JavaScript, so I can only point you in the right direction. It's not a simple topic. If you're really interested, check out http://www.w3schools.com/js/js_obj_htmldom.asp
 
Hi,

thanks very much!
yeah I figured the "both" deal today by a little experimenting.

Thanks for the javascript link!
 
Back
Top