applescript to move 2 kinds of files to different folders - applescript

Hi, I'm trying to create an Applescript to do the following:
Over the week a have several jpg and eps files on my hard drive. I have two folders: Vector and Images.
After some few days I get a few dozens of this mixed files which I have to manually select and move to its respective folder.
How can I move the .eps to the VECTOR folder and the jpg to IMAGES folder.
I donĀ“t know anything about applescripting, so I copied parts of other scripts and put this together:
on run {input, parameters} -- copy
if theExtension is "eps" then
set destination to ~/user/Desktop/VECTO
end if
tell application "Finder"
move anItem to destination
if theExtension is "jpg" then
set destination to ~/user/Desktop/IMAGO
end if
tell application "Finder"
move anItem to destination
end run
Of course it is wrong, but wish someone could help me put this straight
Thnx!

I assume you are incorporating this into an Automator workflow from the way the question is phrased.
on run {input, parameters} -- copy
repeat with aFile in input
tell application "Finder"
if name extension of aFile is "eps" then
move aFile to folder ((path to desktop as text) & "VECTO")
else if name extension of aFile is "jpg" then
move aFile to folder ((path to desktop as text) & "IMAGO")
end if
end tell
end repeat
end run
If not you can use:
set myFiles to (choose file with multiple selections allowed)
repeat with aFile in myFiles
tell application "Finder"
if name extension of aFile is "eps" then
move aFile to folder ((path to desktop as text) & "VECTO")
else if name extension of aFile is "jpg" then
move aFile to folder ((path to desktop as text) & "IMAGO")
end if
end tell
end repeat

Related

Drill Deeper In Recursive Processing

I have a folder that is three levels deep, each level contains a good amount of files. I am able to get two levels deep with the script below, however the third level is posing a bit of a challenge. Would someone mind offering some guidance as to how I am supposed to get one level deeper? Use python or another language wouldn't be acceptable, as I am trying to see how this works with AppleScript.
set sourceFolder to (choose folder)
tell application "Finder"
my changeFileNameCase(sourceFolder, "upper")
repeat with subFolder in (get every folder of folder sourceFolder)
my changeFileNameCase(subFolder as alias, "upper")
#This Is No Good
repeat with theFolder in (get every folder of folder subFolder)
my changeFileNameCase(theFolder, "upper")
end repeat
end repeat
end tell
on changeFileNameCase(targetFolder, caseToSwitchTo)
tell application "Finder"
set fileList to every file of folder targetFolder
repeat with theFile in fileList
set oldName to name of theFile
set newName to my changeCaseOfText(oldName, caseToSwitchTo)
set the name of theFile to newName
end repeat
end tell
end changeFileNameCase
To make the handler recursive you have to get all items of the folder and check the class of each item.
If the class is folder call the handler passing the folder
If the class is file rename it
set sourceFolder to (choose folder)
changeFileNameCase(sourceFolder, "upper")
on changeFileNameCase(targetFolder, caseToSwitchTo)
tell application "Finder"
set theList to every item of targetFolder
repeat with i from 1 to count theList
set theItem to item i of theList
if class of theItem is folder then
my changeFileNameCase(theItem, caseToSwitchTo)
else
set oldName to name of theItem
set newName to my changeCaseOfText(oldName, caseToSwitchTo)
set name of theItem to newName
end if
end repeat
end tell
end changeFileNameCase

Applescript move all files from a specific folder name.

I have a drive that has multiple "Approved" folders with files in them.
The Approved folders are in various locations on the drive.
I need to move the contents of the Approved folders to another directory via applescript.
This is what I have come up with so far but does not seem to be doing the trick, it runs but no files are moved...
Any tips would be great
Thanks
set sourceFolder to "THIS:" as alias
set destinationFolder to "THAT:" as alias
tell application "Finder"
repeat with aFolder in (get folders of sourceFolder)
set folderName to name of aFolder
set filesToMove to (files of sourceFolder whose name = "Approved")
move filesToMove to destinationFolder
end repeat
end tell
According to your description, you are looking for folders named Approved, but your script is searching for files named Approved. Try this:
set sourceFolder to choose folder
set destinationFolder to choose folder
tell application "Finder"
repeat with aFolder in (get folders of sourceFolder)
if aFolder's name is "Approved" then
set filesToMove to (files of aFolder)
move filesToMove to destinationFolder
end if
end repeat
end tell

AppleScript Overwrite or Skip Command?

I have the following applescript that takes files and puts them in their corresponding folders.
set sourceFolder to choose folder
tell application "Finder"
set theFiles to files of sourceFolder
repeat with aFile in theFiles
set fileName to name of aFile
if fileName contains "#" then
set poundOffset to offset of "#" in fileName
set folderName to text 1 thru (poundOffset - 2) of fileName
set newFolder to (sourceFolder as text) & folderName & ":"
if not (exists folder newFolder) then
make new folder at sourceFolder with properties {name:folderName}
end if
move aFile to folder newFolder
end if
end repeat
end tell
It works great for me except in the case of a file conflict. If there's a file with the same name in the folder that it's putting it in the script gets an error and crashes. So my question is this....How do I fix that? I'm willing for it to just overwrite the file but is there a way to bring up a prompt to skip the file or just skip it all together and move on to the next one?
I'm a little fuzzy on what I can do here. Thanks in advance for the help.
Ringslinger.
Try:
move aFile to folder newFolder replacing true

How can I move specific files from within a folder when that folder is added to a directory?

I'm trying to create an applescript that will essentially pull a movie file from a folder when that folder is added to a directory. For instance, when I add a folder containing a movie and other files to my "Movies" directory, I want to be left with the movie file itself, deleting the other unneccesary files.
I've been able to make this work with a manual script but I'd like it to run automatically on add. I think part of the issue is handling the folder coming in. I'm not sure how "these_items" is processed. I've tried handling it as if it were a file and if it was a list of files in the folder.
Here's my attempt so far. I have lines commented out from different trials and I'm using ~/Desktop as my destination folder right now for simplicity.
on adding folder items to this_folder after receiving these_items
tell application "Finder"
--if kind of these_items is "Folder" then
--set this_folder to (path to these_items)
set this_list to every file of these_items
--repeat with i in this_list
--set this_list2 to every file of i
repeat with i from 1 to number of items in this_list
if (name of i) ends with ".mp4" then
move i to (path to desktop folder)
move i to trash
end if
end repeat
--end repeat
--end if
end tell
end adding folder items to
Attach this folder action to your "Movies" directory:
on adding folder items to theFolder after receiving theItems
set destFolder to path to desktop
repeat with anItem in theItems
tell application "Finder"
if kind of anItem is "Folder" then
move (files of (entire contents of anItem) whose name extension = "mp4") to destFolder
delete anItem
else if name extension of anItem = "mp4" then
move anItem to destFolder
else
delete anItem
end if
end tell
end repeat
end adding folder items to

AppleScript to create aliases of files in a folder

So I have this AppleScript for making aliases of all the contents of one folder to another folder.
I works perfectly unless there is only one item in the source folder.
tell application "Finder"
set SourceFolder to (choose folder with prompt "Please choose Source folder:") as string
set DestinationFolder to (choose folder with prompt "Choose Destination folder for aliases:") as string
set theFolders to every item of entire contents of folder SourceFolder
repeat with thisFolder in theFolders
set thisName to name of thisFolder
make new alias file at folder DestinationFolder to thisFolder without invisibles
end repeat
end tell
Any idea why it's not getting anything when there's one item only? When there's at least 2 items in the source folder it creates aliases for both in the destination.
On a side note, any way to get it to remember the source and destination folders between times you run it?
Try:
property SourceFolder : missing value
property DestinationFolder : missing value
if SourceFolder = missing value then
set SourceFolder to (choose folder with prompt "Please choose Source folder:")
set DestinationFolder to (choose folder with prompt "Choose Destination folder for aliases:")
else
tell application "Finder"
set theFolders to every item of entire contents of SourceFolder as list
repeat with thisFolder in theFolders
make new alias file at DestinationFolder to thisFolder
end repeat
end tell
end if

Resources