Apple Script: Move files to appropriate Folder - applescript

I am fairly new to Apple Script and I was hoping to get some help doing this simple but redundant task.
Lets say I have a folder that has these folders
Jabba
Foo
Biggie
and I want to drop files in to this folder, and have it automatically sort 1 of 4 options by just file name.
If file name contains jabba go to the Jabba folder, if foo then Foo folder, ... if none, don't do anything leave it be.
Thanks
I have OSX 10.7.5

Try:
on adding folder items to theFolder after receiving theFiles
repeat with aFile in theFiles
tell application "Finder"
if aFile's name contains "Jabba" then
move aFile to (first folder of theFolder whose name = "Jabba")
else if aFile's name contains "Foo" then
move aFile to (first folder of theFolder whose name = "Foo")
else if aFile's name contains "Biggie" then
move aFile to (first folder of theFolder whose name = "Biggie")
end if
end tell
end repeat
end adding folder items to

Related

AppleScript / FolderAction - Rename files when dropped in folder > Endless Loop

I am trying to do the following with a FolderAction and AppleScript:
Everytime I drop a file on a specific folder it should be renamed and then moved into another folder.
The problem is I get something like an infinite loop (I think) because when the file gets renamed the folder assumes that there is a new file in the folder and so on.
I really don't know how to avoid this and stop the endless loop. Here ist my script:
global newName
set newName to ""
on adding folder items to theAttachedFolder after receiving theNewItems
-- Get the name of the attached folder
tell application "Finder"
set theName to name of theAttachedFolder
-- Count the new items
set theCount to length of theNewItems
-- Display an alert indicating that the new items were received
activate
-- Loop through the newly detected items
repeat with anItem in theNewItems
set oldFileName to name of anItem
-- Rename the file
set the name of anItem to "NewFile" & oldFileName
-- Move the file to other folder
move anItem to "Macintosh HD:Users:blabla:blabla"
end repeat
end tell
tell application "Finder"
delete files of folder "Macintosh HD:Users:user:thisfolder
end tell
end adding folder items to
You are right, renaming the file triggers the folder action again.
The solution is to change the order: First move then rename.
-- Move the file to other folder
set movedItem to move anItem to "Macintosh HD:Users:blabla:blabla"
-- Rename the file
set the name of movedItem to "NewFile" & oldFileName

applescript: automatically delete files in folder if file with same name is present

I have a folder in my google drive where .docx files are shared to continue editing them with Google docs. Google docs now creates documents with the extension .gdoc so that I now have every document twice, once called "test.docx" and "test.docx.gdoc". Now I want to automatically delete the .docx file as soon as the .docx.gdoc file with the same name is present.
I tried this one, but it does not delete the .docx files:
on adding folder items to this_folder after receiving added_items
tell application "Finder"
set docxFiles to every file of this_folder whose name extension is "docx"
repeat with aFile in docxFiles
set baseName to text 1 thru -6 of (get name of aFile)
set gdocFile to baseName & ".docx.gdoc"
if exists gdocFile then delete aFile
end repeat
end tell
end adding folder items to
In my testing I'm relatively sure that everything works up to the line
set gdocFile to baseName & ".docx.gdoc
The line that doesn't do anything seems to be
if exists gdocFile then delete aFile
Any Idea?
This cannot work because you're composing the .docx.gdoc filename without the full path
Try this
on adding folder items to this_folder after receiving added_items
tell application "Finder"
set docxFiles to every file of this_folder whose name extension is "docx"
repeat with aFile in docxFiles
set gdocFile to (aFile as text) & ".gdoc"
if exists file gdocFile then delete aFile
end repeat
end tell
end adding folder items to
I would do it the other way round. Iterate thru the .docx.gdoc files and delete the corresponding docx files.

Move files to folder with the same first 5 characters in Applescript

Suppose I have the following files in folder "A":
"AAAAA 1x1", "AAAAA 1x2", "BBBBB 1x1", "BBBBB 1x2", "CCCCC 1x1", "CCCCC 1x2".
And in folder "B", I have the following folders:
"AAAAA", "BBBBB", "CCCCC".
What I'd like to do is move all the "AAAAA" files in folder "A", to folder "AAAAA" in folder "B", "BBBBB" files to folder "BBBBB", and so on.
How would I do this using Apple Script?
Try running a command like this in Terminal:
for f in A/*; do echo mv "$f" B/${f:2:5}; done
Remove echo to actually move the files.
Try:
set folderA to "/Users/Joao/Desktop/A"
set folderB to "/Users/Joao/Desktop/B"
tell application "System Events"
set subFolders to folders of (folder folderB)
repeat with subfolderB in subFolders
move (files of folder folderA whose name starts with (name of subfolderB)) to (path of subfolderB)
end repeat
end tell
Here's the code (thanks to some fine folks at MacScripter):
set sourceFolder to alias "SSD:Users:JPCanaverde:Documents:A"
set destinationFolder to alias "SSD:Users:JPCanaverde:Documents:B"
tell application "Finder"
repeat with aFolder in (get folders of destinationFolder)
set folderName to name of aFolder
set filesToMove to (files of sourceFolder whose name begins with folderName)
move filesToMove to (contents of aFolder)
end repeat
end tell

Automator/AppleScript to find same file name with different extension in same directory

I'd like to create an automator action to help manage DNG/XMP files. I'd like to be able to drag a DNG (or several) onto the action which will send the DNG and the matching XMP file to the trash. The files have the same name except for the extension, they are in the same directory. For example, IMGP1361.DNG and IMGP1361.xmp
This would be a simple task for a shell script, but is there an Automator way to do it (to learn more about Automator)? Is there a way to get at the file name of the input finder item, change it in a variable and use that as input to another action?
Thanks.
This script for Automator will delete all of the files that share the same prefix and whose name extension is listed in the grep command. You may add additional extensions as well. (xmp|DNG|pdf|xls)
on run {input, parameters}
try
repeat with anItem in input
tell (info for anItem) to set {theName, theExt} to {name, name extension}
set shortName to text 1 thru ((get offset of "." & theExt in theName) - 1) of theName
tell application "Finder"
set parentFolder to parent of anItem as alias
set matchList to every paragraph of (do shell script "ls " & POSIX path of parentFolder & " | grep -E '" & shortName & ".(xmp|DNG)'")
delete (every file of parentFolder whose name is in matchList)
end tell
end repeat
end try
end run
OK, got it. You can use the AppleScript given below inside an Automator workflow like this:
For every selected file in the Finder, if its extension is in ext_list, it will be moved to the Trash, and so will all other files of the same name in the same folder whose extension is one of those in also_these_extensions.
It can be useful, e.g. also for cleaning a folder with auxiliary LaTeX files: just put "tex" into ext_list and all the other extensions (such as "aux", "dvi", "log") into also_these_extensions.
The selected files don't need to be inside the same folder; you can also select several items in a Spotlight search results window.
on run {input, parameters}
-- for every item having one of these extensions:
set ext_list to {"dng"}
-- also process items with same name but these extensions:
set other_ext_list to {"xmp"}
tell application "Finder"
set the_delete_list to {}
set delete_list to a reference to the_delete_list
-- populate list of items to delete
repeat with the_item in input
set the_item to (the_item as alias)
if name extension of the_item is in ext_list then
copy the_item to the end of delete_list
set parent_folder to (container of the_item) as alias as text
set item_name to text 1 thru ((length of (the_item's name as text)) - (length of (the_item's name extension as text))) of (the_item's name as text)
repeat with ext in other_ext_list
try
copy ((parent_folder & item_name & ext) as alias) to the end of delete_list
end try
end repeat
end if
end repeat
-- delete the items, show info dialog
move the_delete_list to the trash
display dialog "Moved " & (length of the_delete_list) & " files to the Trash." buttons {"OK"}
end tell
end run

Applescript: make new folder structure if it doesn't exist

I've been searching through message boards trying to figure out how to execute this script. Essentially the goal is to be able to run this script while inside a folder and if certain folders do not exist, these folders would then be created. If they do already exist, nothing happens. Here's what I've cobbled together so far:
property archivesFolder : "Archives"
property imagesFolder : "Images"
property proofreadFolder : "Proofreading"
property proofFolder : "Proofs"
property sourceFolder : "Source"
try
tell application "Finder" to set theLocation to (folder of the front window as alias)
end try
tell application "Finder"
if (exists folder archivesFolder) then
(* do nothing *)
else
make new folder at theLocation with properties {name:archivesFolder}
end if
if (exists folder imagesFolder) then
(* do nothing *)
else
make new folder at theLocation with properties {name:imagesFolder}
end if
if (exists folder proofreadFolder) then
(* do nothing *)
else
make new folder at theLocation with properties {name:proofreadFolder}
end if
if (exists folder proofFolder) then
(* do nothing *)
else
make new folder at theLocation with properties {name:proofFolder}
end if
if (exists folder sourceFolder) then
(* do nothing *)
else
make new folder at theLocation with properties {name:sourceFolder}
end if
end tell
What am I doing wrong ? (forgive my n00b code formatting, at work and can't figure out how to create code blocks) Also, is it possible to this not just on the front window, but on a folder that is just selected? Any help given would be awesome.
I suggest two options (to run the script):
Option 1: Take that code (assuming it does what you plan), and save it as an application (with Script Editor).
Then, just drag that application to your window toolbar (you need to have the toolbar visible). To do that, hold the command key while dragging.
Option 2: Use Butler: http://manytricks.com/butler/
(there is a free version, I don't know your OSX version).
It allows you to define system-wide shortcut keys for applescript scripts.
Create a smart item (applescript); paste the code there, and in the name of the script add the shortcut keys: example: create folder here ⇧⌥⌘N
EDIT:
According to your comment, I understand your problem and I can tell you that you were missing the path (the current folder, in your case theLocation)
So, in every case of if (exists folder archivesFolder) then you need to add the of theLocation like this: if not (exists folder archivesFolder of theLocation) then
Finally, knowing that you won't do any thing if the folder exists, just test the false case.
I tested this code and am posting it here:
property archivesFolder : "Archives"
property imagesFolder : "Images"
property proofreadFolder : "Proofreading"
property proofFolder : "Proofs"
property sourceFolder : "Source"
try
tell application "Finder" to set theLocation to (folder of the front window as alias)
end try
tell application "Finder"
if not (exists folder archivesFolder of theLocation) then
make new folder at theLocation with properties {name:archivesFolder}
end if
if not (exists folder imagesFolder of theLocation) then
make new folder at theLocation with properties {name:imagesFolder}
end if
if not (exists folder proofreadFolder of theLocation) then
make new folder at theLocation with properties {name:proofreadFolder}
end if
if not (exists folder proofFolder of theLocation) then
make new folder at theLocation with properties {name:proofFolder}
end if
if not (exists folder sourceFolder of theLocation) then
make new folder at theLocation with properties {name:sourceFolder}
end if
end tell
You can also use a shell script with mkdir, since the option to create intermediate folders won't error if the folder already exists.
# define a list of folders - items will need to be quoted if they contain spaces, etc.
property theFolders : {"Archives", "Images", "ProofReading", "Proofs", "Source"} -- can also nest, e.g. "Inside/One"
try
tell application "Finder" to set targetFolder to (target of the front window) as alias
on error -- no window
set targetFolder to (choose folder)
end try
# build a parameter string from the folder list
set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space}
set {theFolders, AppleScript's text item delimiters} to {theFolders as text, tempTID}
do shell script "cd " & quoted form of POSIX path of targetFolder & "; mkdir -p " & theFolders
Try this:
tell application "Finder"
set theLocation to (target of front window) as string
set folderNames to {"archivesFolder", "imagesFolder", "proofreadFolder", "proofFolder", "sourceFolder"}
repeat with theFolder in folderNames
try
make new folder at theLocation with properties {name:"" & theFolder & ""}
end try
end repeat
end tell
This is a script I did to sort a bunch of medical files into subfolders based on the date of service (explaining the "text 5 thru 14" in the script; the files are named according to a pattern so the script can extract the date of service from the filename). Rather than check if the folder already exists, I just put the make folder instruction in a try block; if the folder already exists, the 'try' fails, but the script continues under the assumption that the folder already exists. Using the 'text' item instead of 'character' returns the extracted string as a single string and not as an array of characters that have to be converted back into a string.
tell application "Finder"
set theDirectory to "Internal Disk:Users:steven:Documents:Vondalee:Medical"
set theFiles to every file in folder theDirectory whose name extension is "pdf"
repeat with eachFile in theFiles
set theFileName to the name of eachFile
set theFolder to text 5 thru 14 of theFileName
try
make new folder at theDirectory with properties {name:theFolder}
end try
move eachFile to folder theFolder of folder theDirectory
end repeat
end tell

Resources