View Full Version : [AppleScript] Duplicate files with timestamp
Mikuro
March 16th, 2008, 02:17 PM
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.
iaeon
September 21st, 2008, 10:34 AM
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.
Mikuro
September 21st, 2008, 11:30 AM
Hi iaeon.
There are different ways you could install it. I like to use the Script menu. I decided to write a how-to on enabling and using the Script menu, and I just posted it here: http://macosx.com/forums/howto-faqs/303151-use-applescript-menu.html#post1450790
In short, I have it set up so that I select the file I want to duplicate in the Finder, then choose my script from the Script menu.
iaeon
September 21st, 2008, 01:07 PM
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 Â
Mikuro
September 21st, 2008, 02:14 PM
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.
iaeon
September 21st, 2008, 04:00 PM
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.
ejang
February 20th, 2009, 10:50 PM
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.
Mikuro
February 21st, 2009, 09:49 AM
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.
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.)
ejang
February 21st, 2009, 02:42 PM
awesome
thank you so much!
I'm just curious, but how would I configure the file to duplicate itself to other folders?
ejang
February 21st, 2009, 03:47 PM
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!
Mikuro
February 21st, 2009, 04:12 PM
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: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 (http://developer.apple.com/documentation/applescript/Conceptual/AppleScriptLangGuide/introduction/ASLR_intro.html) 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.
ejang
February 21st, 2009, 06:11 PM
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
ElDiabloConCaca
February 22nd, 2009, 10:12 AM
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.
ejang
February 22nd, 2009, 07:53 PM
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
ejang
February 27th, 2009, 09:34 PM
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.
Mikuro
February 27th, 2009, 11:03 PM
The duplicate command returns a reference to the new copy. So you just need to store it somewhere.
Try this: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).
ejang
March 22nd, 2009, 01:06 PM
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
ejang
March 24th, 2009, 03:05 AM
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!
Mikuro
March 24th, 2009, 03:21 PM
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: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
ejang
March 24th, 2009, 07:56 PM
Hi,
thanks very much!
yeah I figured the "both" deal today by a little experimenting.
Thanks for the javascript link!
ejang
March 24th, 2009, 08:16 PM
Hi once again,
I don't think this is possible, but I just wanted to check to see if this works at all...
I have an application that sends itself to the trash. Is there any way I can simultaneously quit that application and empty the trash at the same time? I can see how this can be done if I used a double-script file: one that opens the both of them, one that executes its process, then deleting itself. Then I can see how a timed delay can be integrated into the first script so that it empties the trash right after the second script finishes. Does anybody know how I can do this?
Pertaining to this question, I have thought of an abstract solution, but it is extremely inefficient and I am sure there is a faster way. One way to integrate these "double" scripts is to store one on the internet, have one script download the other and then open it automatically. This should be pretty easy except I don't know how to open the resulting downloaded file, on top of selecting "open" when the internet-application confirmation prompt appears on screen.
Thanks!
somotorcycle
June 29th, 2009, 11:23 AM
Hi Mikuro,
Thanks for all your help on this forum. You're certainly adding a valuable service.
So I need to rename a file twice a day with one constant field and the current time stamp, e.g. s3759_20090629_0713. s3759 needs to stay constant and the date and time stamps follow in the above form.
Will you please advise me how to change your script to provide stamps in this format? After renaming the file, I'll need to have it automatically upload via ftp to a server.
Thanks for your help!
Andrew
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by
vBSEO 3.3.0 RC1