Automatically renaming downloaded files on Mac - macos

I've been trying to find a way to automatically rename my downloaded files on my Macbook Pro, but I somehow can't. I've tried using Automator and having a folder action that renames the files coming into the folder, but somehow it doesn't work at all, as if it was disabled.
Does anybody have any idea on how I could rename them automatically when they're downloaded, so that it's easier to keep them in order (mainly for myself), for archiving reasons.
The way I want to rename them is simply to add the creation date to it, just like this script should work.
Folder Script Automator
However, this does rename them, but it never stops, it keeps adding the date in eternity, and of course I'd only like it once in the beginning.
What it really does

Save this following AppleScript code in Script Editor.app as “Move And Rename.scpt” to your folder… /Users/YOUR SHORT NAME/Library/Workflows/Applications/Folder Actions/
To be able to rename the files using a folder action, the files to be renamed must be moved to a different folder otherwise it will create an infinite loop
The only thing you need to do is create a folder in your downloads folder and name it … Renamed Files. This is where the renamed files will be placed
on adding folder items to theFolder after receiving theNewItems
tell application "Finder" to set theNewItems to files of folder theFolder
repeat with i from 1 to count of theNewItems
set theFile to item i of theNewItems
set moveToFolder to (path to downloads folder as text) & "Renamed Files:"
set AppleScript's text item delimiters to ","
set theLongDate to (current date)
set theLongDate to (date string of theLongDate)
set currentMonth to (word 1 of text item 2 of theLongDate)
set currentDay to (word 2 of text item 2 of theLongDate)
set currentYear to (word 1 of text item 3 of theLongDate)
set monthList to {January, February, March, April, May, June, ¬
July, August, September, October, November, December}
repeat with x from 1 to 12
if currentMonth = ((item x of monthList) as string) then
set theRequestNumber to (text -2 thru -1 of ("0" & x))
exit repeat
end if
end repeat
set currentMonth to theRequestNumber
set currentDay to (text -2 thru -1 of ("0" & currentDay))
set theShortDate to (currentYear & "-" & currentMonth & "-" & currentDay) as string
set newName to theShortDate
tell application "Finder"
set theName to name of theFile
move theFile to moveToFolder
set theFile to moveToFolder & theName
try
set name of alias theFile to newName & " " & theName
on error errMsg number errNum
set name of alias theFile to newName & " 1 " & theName
end try
end tell
end repeat
end adding folder items to
Once you have saved that file to that location, it will then be ready to attach to any folder you choose in Finder.app as a folder action

Related

Make multiple folders from excel using AppleScript with specific naming structure

I need to make thousands of folders from columns from excel. I've looked around the internet and have got close, but I need the naming structure to be
"LASTNAME, FIRSTNAME - ID"
Each of those are in a separate column in excel.
I've tried the code below and it works, but the naming structure is not what I want it to be.
display dialog " --Make Lotsa' Folders--
This AppleScript will create multiple folders using names from a text file that you specify."
set destination to (choose folder with prompt "Where would you like to make the folders?")
set textFile to (choose file with prompt "Select the text file you wish to use")
set folderNames to paragraphs of (read textFile)
repeat with oneName in folderNames
tell application "Finder" to make new folder at destination with properties {name:oneName}
end repeat
display dialog "Your folders are done." buttons {"OK"}
https://imgur.com/a/T2ske
https://imgur.com/a/L2al5 This is what the folder filename would be.
Please try this:
set destination to (choose folder with prompt "Where would you like to make the folders?")
tell application "Microsoft Excel" to set usedRange to string value of used range of sheet 1
repeat with i from 2 to (count usedRange)
tell item i of usedRange to set folderName to item 1 & ", " & item 2 & " - " & item 3
tell application "Finder" to make new folder at destination with properties {name:folderName}
end repeat
display dialog "Your folders are done." buttons {"OK"}
As mentioned in the comments the Finder is very very slow while creating the folders one by one, this is a faster solution keeping the way to parse Excel (which is pretty fast by the way) but using the shell mkdir command with a composed parameter list to create the folders.
set destination to (choose folder with prompt "Where would you like to make the folders?")
tell application "Microsoft Excel" to set usedRange to string value of used range of sheet 1
set nameList to {}
repeat with i from 2 to (count usedRange)
tell item i of usedRange to set end of nameList to "'" & item 1 & ", " & item 2 & " - " & item 3 & "'"
end repeat
set {TID, text item delimiters} to {text item delimiters, ","}
set mkdirParams to "/{" & (nameList as text) & "}"
set text item delimiters to TID
do shell script "/bin/mkdir " & quoted form of POSIX path of destination & mkdirParams
display dialog "Your folders are done." buttons {"OK"}

Trouble with script moving files to a newly created folder in Applescript

I am having trouble with what I think is a pretty straightforward task, but cannot seem to get my script to work correctly. I have found a lot of help through the forums with regards to the individual routines used, but it still seems to fail.
In short, what I would like to do is monitor a folder for new files being added. Once a batch of files are added (every other day or so), it will create a folder in another directory with the new folder name being the current date, move those files to the new directory, and then execute a simple bash script which uses the new directory name as an argument.
My script compiles ok, but once files are added it only creates the new folder and nothing else. I appreciate any help.
property the_sep : "-"
on adding folder items to my_folder after receiving the_files
tell application "Finder"
(* First create a new folder with name of folder = current date *)
set the_path to (folder "qa" of folder "Documents" of folder "ehmlab" of folder "Users" of disk "Macintosh HD")
set the_name to (item 1 of my myDate())
set the_name to (the_name & the_sep & item 2 of my myDate())
set the_name to (the_name & the_sep & item 3 of my myDate())
make folder at the_path with properties {name:the_name}
set newDir to the_path & the_name
end tell
(* Next, move the newly added files to the source into the newly created date folder *)
repeat with i from 1 to number of items in the_files
tell application "Finder"
try
set this_file to (item i of the_files)
move file this_file to folder newDir
end try
end tell
end repeat
do shell script "qc.sh " & newDir
end adding folder items to
on myDate()
set myYear to "" & year of (current date)
set myMth to text -2 thru -1 of ("0" & (month of (current date)) * 1)
set myDay to text -2 thru -1 of ("0" & day of (current date))
return {myYear, myMth, myDay}
end myDate
The failure reason are different path styles.
AppleScript uses HFS paths (colon separated).
UNIX uses POSIX paths (slash separated).
The solution is to coerce the HFS path string to POSIX path
do shell script "qc.sh " & quoted form of POSIX path of newDir
This is a shorter version of the script using the shell also for the time stamp and for creating the directory.
on adding folder items to my_folder after receiving the_files
(* Next, move the newly added files to the source into the newly created date folder,
"path to documents" is a relative path to the documents folder of the current user *)
set baseFolder to (path to documents folder as text) & "qa:"
set timeStamp to do shell script "date +%Y-%m-%d"
set newDir to baseFolder & timeStamp
do shell script "mkdir -p " & quoted form of POSIX path of newDir
(* Next, move the newly added files to the source into the newly created date folder *)
repeat with aFile in the_files
tell application "Finder"
try
move aFile to folder newDir
end try
end tell
end repeat
do shell script "qc.sh " & quoted form of POSIX path of newDir
end adding folder items to

AppleScript dead slow in moving files to folder

I'm trying to move a bunch of files into folders, based on the file creation date. Using the script below, the rate of move is about 1 file per second. 64000 files to go.
Script timed out when I tried
set myFiles to items of myFolder whose name ends with "jpg"
repeat with aFile in myFiles
So now I process file by file and only checking folder existing when the date comparison failed sped up the process only marginally. I've tried the obvious
set myDayBuddies to items of myFolder whose creation date is theFileDate
but this too, did not help really much. I suspect Finder reads the whole folder content time and time again, creating huge overhead.
What is the proper method of speeding up this any further? In PHP I know I can read entries from a directory stream, one by one.
Script:
tell application "Finder"
set myFolder to choose folder
set prevDateString to ""
repeat while true
try
set aFile to first item of myFolder whose name ends with "jpg"
set theFileDate to (the creation date of aFile)
set theDateString to my composedate(theFileDate)
if (theDateString is not prevDateString) then
if not (exists folder ((myFolder & theDateString) as text)) then
make new folder at myFolder with properties {name:theDateString}
end if
set prevDateString to theDateString
set destPath to (((myFolder as text) & theDateString) as alias)
end if
move aFile to destPath
on error
return
end try
end repeat
end tell
on composedate(aDate)
set y to (year of aDate as integer) as string
set m to (month of aDate as integer) as string
set d to (day of aDate as integer) as string
if length of m < 2 then
set m to "0" & m
end if
if length of d < 2 then
set d to "0" & d
end if
set myDateString to y & m & d
return myDateString
end composedate
System Events was created to take much of the load off the Finder. The Finder is slow so use system events when possible. I'm not sure how it will perform on a folder with 64000 files but give it a try.
set myFolder to choose folder
tell application "System Events"
set f to files of myFolder whose name extension is "jpg"
end tell
And if system events is still too slow then applescript alone isn't appropriate for such a large folder. You'll have to go to using the command line even though you said you don't want to. Something like this should be faster...
set myFolder to (choose folder) as text
set jpgFiles to paragraphs of (do shell script "ls " & quoted form of POSIX path of myFolder & " | grep 'jpg$'")
repeat with aFile in jpgFiles
set filePath to myFolder & aFile
tell application "System Events"
set theFileDate to creation date of file filePath
-- do more stuff
end tell
end repeat

(Automator/AppleScript) Rename files to a folder name, save to different folder & add prefix

I really need you help and will be very grateful for it.
I think this should be no problem for you.
So I have folder with different subfolders in it. In subfolders are images.
What I need is:
Change each image name to it’s subfolder name + 001 and so on («1stSubfolder001, 1stSubfolder002, …», «2ndSubfolder001, 2ndSubfolder002, …»)
Move all images from all subfolders to one folder (or at least to root folder)
Add random prefix number to names.
I have script to third task:
tell application "Finder"
repeat with this_item in (get items of window 1)
set name of this_item to ((random number from 0 to 99999) & name of this_item) as string
end repeat
end tell
But it only works with opened folder on the foreground. Maybe there is a way to make one script to run it on the background?
Huge thank you in advance.
I found cool automator app here, but I need to correct it a little bit.
How do I use the current folder name without a path as a variable in automator in Mac?
This script does everything I need BUT it renames all files to ONE folder name, not each different.
Here is another applescript that does rename files to folders, but it uses 2 folders name (folder + subfolder), and I need only subfolder prefix.
set myFolder to do shell script "sed 's/\\/$//' <<< " & quoted form of POSIX path of (choose folder)
set myFiles to paragraphs of (do shell script "find " & quoted form of myFolder & " \\! -name \".*\" -type f -maxdepth 2 -mindepth 2")
repeat with aFile in myFiles
tell application "System Events" to set file aFile's name to (do shell script "sed 's/.*\\/\\([^/]*\\)\\/\\([^/]*\\)\\/\\([^/]*$\\)/\\1_\\2_\\3/' <<< " & quoted form of aFile)
end repeat
(MacBook Pro Late 2013, OS X Yosemite 10.10.1)
This accomplishes what you want. You should be able to adjust from here. There are no checks to ignore non-image files.
property top_folder : alias "Macintosh HD:Users:MyName:Downloads:Images:"
property save_folder : ""
set save_folder to choose folder with prompt "Select the folder to save the images in."
process_folder(top_folder)
on process_folder(this_folder)
set these_items to list folder this_folder without invisibles
set container_name to name of (info for this_folder)
repeat with i from 1 to the count of these_items
set this_item to alias ((this_folder as Unicode text) & (item i of these_items))
if folder of (info for this_item) is true then
process_folder(this_item)
else
process_item(this_item, container_name, i)
end if
end repeat
end process_folder
-- this sub-routine processes files
on process_item(this_item, c, i)
-- make the integer a 3 digit string
if i < 10 then
set i to "00" & i
else if i < 100 then
set i to "0" & i
end if
-- set a random number
set r to (random number from 0 to 99999) as string
tell application "System Events"
-- get file extension so not overwritten
set e to name extension of this_item
set new_name to "" & r & "_" & c & "_" & i & "." & e
set name of this_item to new_name
move this_item to save_folder
end tell
end process_item

Applescript - move files to folder which name begins with first 2 characters of filename

I'd like a little help fine tuning this script. Is it possible to have this script match subfolders based on the first two letters of file? Parent folder would be "Shots" which contains subfolders with unique two letter prefix "BA_Bikini_Atol" which contains subfolders within those folders for the specific shots ba_0020, ba_0030, etc. Would like to move a file, ba_0020_v0002 to the ba_0020 folder by selecting "shots" as the target and the script looks through all the sub folders for a match. Thoughts?
set mgFilesFolder to (choose folder with prompt "Where are the files stored which you would like to move to the destination?")
set mgDestFolder to (choose folder with prompt "Where is the destination folder?")
tell application "System Events"
set folderList to name of folders of mgDestFolder
set fileList to name of files of mgFilesFolder
end tell
repeat with i from 1 to (count folderList)
set folderName to item i of folderList
set filesToMove to {}
repeat with j from 1 to (count fileList)
set filename to item j of fileList
if filename begins with folderName then
set end of filesToMove to alias ((mgFilesFolder as string) & filename)
end if
end repeat
tell application "Finder"
move filesToMove to alias ((mgDestFolder as string) & folderName & ":")
end tell
end repeat
This works in my tests
set mgFilesFolder to (choose folder with prompt "Where are the files stored which you would like to move to the destination?")
set mgDestFolder to (choose folder with prompt "Where is the destination folder?")
(* get the files of the mgFilesFolder folder *)
tell application "Finder" to set fileList to files of mgFilesFolder
(* iterate over every file *)
repeat with i from 1 to number of items in fileList
set this_item to item i of fileList
set this_file_Name to name of this_item as string
(* get the file name code
Using the delimiter "_" break the name into fields of text and returning fields 1 thru 2 . i.e ba_0030 *)
set thisFileCode to (do shell script "echo " & quoted form of this_file_Name & " |cut -d _ -f 1-2")
log thisFileCode
tell application "Finder"
set folderList to ((folders of entire contents of mgDestFolder) whose name starts with thisFileCode)
if folderList is not {} then
move this_item to item 1 of folderList
end if
end tell
end repeat
FROM:
TO:
I've made (with the help from users of this site) the script you're referring to.
I've made similar (more efficient) scripts since then. Most of them depend on ASObjC Runner which you can get here: Download ASObjC Runner
If you have it installed I think the following script will work:
set mgFilesFolder to (choose folder with prompt "Where are the file stored which you would like to move?")
set mgDestFolder to (choose folder with prompt "Where are destination folders?")
tell application "ASObjC Runner"
set mgFiles to enumerate folder mgFilesFolder with recursion without including folders
repeat with mgFile in mgFiles
set mgPrefix to text 1 thru 7 of name of (about file mgFile include only "name")
set mgDest to enumerate folder mgDestFolder match prefix mgPrefix with recursion
manage file mgFile moving into mgDest
end repeat
end tell

Resources