I save documents from the web frequently in that have file names that already exist. What I would like to accomplish is to write a script that will automate the process of saving a file to a specified folder but change the name to a random string of numbers or characters. Ideally I would be able to do this by holding down a specific key or two plus a mouse click. Can this be done? If so can you offer any tips for an applescript newbie?
Thanks!
Taylor
Model: iMac
AppleScript: 2.1.1 (81)
Browser: Safari 419.3
Operating System: Mac OS X (10.4)
Heres a couple of ideas/tips for you to start with!
Heres a way of making a folder with a random number:
set Random_name to random number from 5000 to 9999
tell application "Finder"
set new_folder to make new folder with properties {name:Random_name}
end tell
if you want to add a keyboard stroke to run an applescript theres a few to choose from in the way of applications but
my favourite is “fastscripts” do a google search for it!!
Thanks for the tips. To save the files with a random name in a set and constant folder I would assume some minor changes would be needed to the script.
Key/Mouse combo - say shift + click triggers the script
The document is downloaded and saved in a set folder with a name ranging from {1-9999} or any given range. The document type is preserved (Excel, Word, etc).
Even better the script would not allow for duplicate names and would keep trying random numbers until a unique name was determined.
This would get rid of the annoying “would you like to replace the file” issues.
An easy way to do this, using pidge’s example, is a repeat loop which will be left, when a new random name could be created
repeat
set Random_name to random number from 5000 to 9999
try
tell application "Finder" to set new_folder to make new folder with properties {name:Random_name}
exit repeat
end try
end repeat
I made some minor changes to have the script create a new file with a random name v/s a new folder. Since this is my first script the changes are based on my best guess at what needs to be done. My changes are in bold.
There are only 3 additional modifications that I need to make (assuming that my changes are ok).
1 - set location to a specific folder. I want the script to save each file in a download folder.
2 - assign a shortcut to run the script. Assuming that it is unused I would like to use [SHIFT + MOUSE CLICK] to execute the script.
3 - preserve file type. If I am saving an Excel file I would want to preserve the .XLS file extension (or any file type)
So if I want to save a file I would [SHIFT + CLICK] on the file, the script would run looking for a unique file name and saving the file in a specific folder.
repeat
set Random_name to random number from 1000 to 999999
try
tell application “Finder” to set new_file to make new file with properties {name:Random_name}
exit repeat
end try
end repeat
This should be no problem.
But I’ve also two questions:
¢ What is this new file for?
¢ Where do the files to rename with the random name and to save come from?
The files are primarily for researching projects online. Most of the duplicates come from file names like workbook1.xls or 1.bmp that have to be renamed to avoid overwriting existing files. This slows things down and introduces the risk that I accidentally overwrite important files or photos.
The files would be from Safari and possibly FireFox. If we can get this to work in Safari only that is just fine but if it could work for both browsers that would be better.
The renaming issue pops up when importing photos but I can use a mass re-name in iPhoto to address this problem.
If I understand the question - the current process would be to CONTROL + CLICK, “download linked file as”, select a location, and rename the file if a duplicate exists. The new process would be to use a shortcut SHIFT + CLICK (or other) to automate the whole process.
This is a different approach.
It checks before downloading the file whether the filename already exists at destination
and creates a new file name by appending “-1”. If this name also exists the counter will be incremented
until a unique file name can be created
If the mouse is over a link in Safari, the URL will be displayed in the status line at the bottom of the window
The script extracts the URL and downloads the file using curl
property destinationFolder : missing value
if destinationFolder is missing value then set destinationFolder to choose folder
activate application "Safari"
try
tell application "System Events"
tell process "Safari"
set StatusBarOff to title of (get 1st menu item of menu 1 of menu bar item 5 of menu bar 1 whose title contains "Status") contains "Show"
if StatusBarOff then display dialog "Enable status bar in Safari and run script again" buttons {"Cancel"} default button 1
set theLink to value of static text 1 of group 1 of window 1
end tell
end tell
if theLink is not "" then
set {TID, text item delimiters} to {text item delimiters, ASCII character 210}
set theLink to text item 2 of theLink
set text item delimiters to ASCII character 211
set theLink to text item 1 of theLink
set text item delimiters to TID
set linkFileName to do shell script "basename " & quoted form of theLink
do shell script "curl -o " & quoted form of POSIX path of checkFileName(destinationFolder, linkFileName, "-") & " " & quoted form of theLink
end if
end try
on checkFileName(fDir, fName, separator) -- fDir can be alias or path string
set fDir to fDir as Unicode text
try
set f to (fDir & fName) as alias
set {name:Nm, name extension:Ex} to info for f
if Ex is missing value then set Ex to ""
if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
set idx to 0
repeat
set idx to idx + 1
set checkName to (fDir & Nm & separator & (idx as string) & "." & Ex)
try
checkName as alias
on error
return checkName
end try
end repeat
on error
return (fDir & fName)
end try
end checkFileName
To trigger the script, you need a tool like FastScripts, Butler, QuickSilver, QuicKeys etc.