I have an Applescript question that is much more complex than I can construct. I have been searching for the past couple of days, and I cannot find any script like this, nor can I find enough information to piece one together with my limited knowledge.
I have multiple files with structured names. Each file has the following name structure:
ttu_collectionname_000001.pdf
ttu_collectionname_000002.mp3
ttu_collectionname_000003.pdf ... etc. (Each of these files are of varying file types.)
There is also a csv metadata file associated with each of the original files.
ttu_collectionname_000001.csv
ttu_collectionname_000002.csv
ttu_collectionname_000003.csv ... etc. (Each of these files are csv files.)
I need to create a folder based on the name of the file with sub and sub-subfolders. Each top-level folder name will be unique in the number sequence. Each sub and sub-subfolder name will be the same for each top-level folder.
The folder structure should look like this:
ttu_collectionname_000001
content
archive
display
metadata
archive
display
ttu_collectionname_000002
content
archive
display
metadata
archive
display
I then need to move the each file to a particular sub-subfolder.
The file ttu_collectionname_000001.pdf would be moved to the ttu_collectionname_000001/content/display folder.
The file ttu_collectionname_000001.csv would be moved to the ttu_collectionname_000001/metadata/display folder.
Try:
set myFolder to "Mac OS X:Users:stark:Main Folder"
tell application "Finder" to set myFiles to folder myFolder's files as alias list
repeat with aFile in myFiles
tell application "System Events" to set {fileName, fileExt} to {name, name extension} of aFile
set baseName to text 1 thru ((get offset of "." & fileExt in fileName) - 1) of fileName
do shell script "mkdir -p " & (quoted form of (POSIX path of myFolder)) & "/" & baseName & "/{\"content\",\"metadata\"}/{\"display\",\"archive\"}"
tell application "System Events"
if fileExt is "pdf" then move aFile to (myFolder & ":" & baseName & ":content:display" as text)
if fileExt is "csv" then move aFile to (myFolder & ":" & baseName & ":metadata:display" as text)
end tell
end repeat
Assuming your files are in the same folder,
this AppleScript:
set pathToFolderOfTTUFiles to (path to the desktop as text) & "TTU:"
tell application "Finder"
set theFiles to every item of folder pathToFolderOfTTUFiles whose name extension is not "csv" and kind is not "Folder"
repeat with theFile in theFiles
set lengthOfExtension to (length of (theFile's name extension as text)) + 1
set fileNameWithoutExtension to text 1 through -(lengthOfExtension + 1) of (theFile's name as text)
set theFolder to make new folder at folder pathToFolderOfTTUFiles with properties {name:fileNameWithoutExtension}
set theContentFolder to make new folder at theFolder with properties {name:"content"}
make new folder at theContentFolder with properties {name:"archive"}
set theContentDisplayFolder to make new folder at theContentFolder with properties {name:"display"}
set theMetadataFolder to make new folder at theFolder with properties {name:"metadata"}
make new folder at theMetadataFolder with properties {name:"archive"}
set theMetadataDisplayFolder to make new folder at theMetadataFolder with properties {name:"display"}
move theFile to theContentDisplayFolder
set pathToCSV to pathToFolderOfTTUFiles & fileNameWithoutExtension & ".csv"
if exists pathToCSV then move pathToCSV to theMetadataDisplayFolder
end repeat
end tell
creates this:
Related
Say I have a bunch of folders with similar subpaths:
Folder 1
Do
Re
Mi
<Files>
Folder 2
Do
Re
Mi
<Different Files>
I know I can use tell application Finder to open "Macintosh HD:Users: ... to open a folder. Is there any way to queue a popup to select which of Folder 1 or Folder 2 I would like to enter, then input it into the above command? For instance, if I select Folder 1, it would go to ...Folder 1/Do/Re/Mi, whereas if I select Folder 2, it would go to ...Folder 2/Do/Re/Mi.
One thing I tried is combining do shell script with concatenate to get do shell script "open " & variable & "/Do/Re/Mi/", but the code fails if the file name is more than one word.
Set a base folder
Retrieve all folder names of the base folder
Create a list
Choose an item
Concatenate the path and open the folder
For example (set baseFolder to the alias of the parent folder of Folder 1 and Folder 2)
set baseFolder to path to home folder
set folderList to {}
tell application "Finder" to set allFolders to name of folders of baseFolder
repeat with i from 1 to (count allFolders)
set end of folderList to (i as text) & space & item i of allFolders
end repeat
set chosenItem to choose from list folderList
if chosenItem is false then return
set chosenIndex to word 1 of item 1 of chosenItem
set selectedFolder to item chosenIndex of allFolders
set destinationFolder to (baseFolder as text) & selectedFolder & ":Do:Re:Mi:"
tell application "Finder"
if exists folder destinationFolder then
open folder destinationFolder
else
display dialog "Sorry, the folder does not exist"
end if
end tell
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
Here's what I'm trying to do.
I've got a file structure that contains photos in both JPG and RAW formats. It's a folder called "Photos" with subfolders by date. I'd like to copy just the RAW photos to a new folder, "Photos RAW", but keep the structure by date taken/created.
I can copy just the files using automator or applescript a directory to a new one, but how do I walk the directory tree using applescript so I cover all the subfolders?
Try this. You'll see I used "entire contents" to get the files in the subfolders too.
set extensionToFind to "raw"
set topLevelFolder to (choose folder) as text
set pathCount to count of topLevelFolder
tell application "Finder"
-- get the files
set rawFiles to files of entire contents of folder topLevelFolder whose name extension is extensionToFind
if rawFiles is {} then return
-- setup the folder where the files will be moved
set rawFolder to ((container of folder topLevelFolder) as text) & "Photos_Raw:"
do shell script "mkdir -p " & quoted form of POSIX path of rawFolder
repeat with aFile in rawFiles
set aFileContainer to (container of aFile) as text
if topLevelFolder is equal to aFileContainer then
-- here the file is at the top level folder
set newPath to rawFolder
else
-- here we calculate the new path and make sure the folder structure is in place
set thisFile to aFile as text
set subFolderPath to text (pathCount + 1) thru -((count of (get name of aFile)) + 1) of thisFile
set newPath to rawFolder & subFolderPath
do shell script "mkdir -p " & quoted form of POSIX path of newPath
end if
move aFile to folder newPath
end repeat
end tell
Basically, I'm trying to create a folder action in Automator so whenever a file is added to a specific folder, it will create a subfolder that matches the filename (without the extension), then move the file into that subfolder.
So far, I have successfully used a post from this site (Create new folder named with a specified file name in Automator) to create a script that will create the new folder. However, I have been unable to modify the script to move the original file into the new folder.
Any help would be appreciated. Here is the full script I am working with for reference:
on run {input, parameters} -- make new folders from base file names
set output to {}
repeat with anItem in the input -- step through each item in the input
set anItem to anItem as text
tell application "System Events" to tell disk item anItem
set theContainer to path of container
set {theName, theExtension} to {name, name extension}
end tell
if theExtension is in {missing value, ""} then
set theExtension to ""
else
set theExtension to "." & theExtension
end if
set theName to text 1 thru -((count theExtension) + 1) of theName -- the name part
tell application "Finder"
make new folder at folder theContainer with properties {name:theName}
set end of output to result as alias
end tell
end repeat
return input -- or output
end run
Thanks in advance!
Add this folder action to your target folder:
on adding folder items to theFolder after receiving theFiles
repeat with aFile in theFiles
tell application "System Events" to set {Nm, Ex, pPath} to aFile's {name, name extension, POSIX path of container}
set BN to text 1 thru ((get offset of "." & Ex in Nm) - 1) of Nm
set thePath to (pPath & "/" & BN & "/" as text)
do shell script "mkdir -p " & quoted form of thePath
delay 0.5
tell application "Finder" to move aFile to POSIX file thePath
end repeat
end adding folder items to
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