Error making folders in Applescript - applescript

So, i'm getting an error while running this:
on hazelProcessFile(theFile)
set text item delimiters to ":"
set filename to last text item of (theFile as text)
set text item delimiters to "."
if filename contains "." then
set base to text items 1 thru -2 of filename as text
set extension to "." & text item -1 of filename
else
set base to filename
set extension to ""
end if
set text item delimiters to {"720p", "HDTV", "x264", "IMMERSE", "-", "E01", "…", "E02", "EVOLVE"}
set ti to text items of base
set text item delimiters to ""
set newbase to ti as text
set newbase to Replace(newbase, "S0", "Season ")
set newbase to Replace(newbase, ".", " ")
set newbase to Replace(newbase, " ", "")
set newbase to Replace(newbase, " ", "")
set folderLocation to "/Volumes/LaCie/Midia/Series/"
set folderName to newbase as text
tell application "Finder"
if newbase contains "Season" then
if not (exists folder (POSIX file "/Volumes/LaCie/Midia/Series/" & newbase as text)) then
set p to path to "/Volumes/LaCie/Midia/Series/"
--make new folder at p with properties {name:newbase}
make new folder with properties {name:folderName, location:p}
else
move theFile to POSIX file "/Volumes/LaCie/Midia/Series/" & newbase as text
set name of result to newbase
end if
else
move theFile to POSIX file "/Volumes/LaCie/Midia/Filmes/"
end if
end tell
end hazelProcessFile
on Replace(input, x, y)
set text item delimiters to x
set ti to text items of input
set text item delimiters to y
ti as text
end Replace
Specifically in the part where it is suposed to create a folder if it doesnt exists (its purpose is to automatically sort tv series episodes).
Log file:
NSLocalizedDescription = "Finder got an error: Can\U2019t make \"/Volumes/LaCie/Midia/Series/\" into type constant.";
NSLocalizedFailureReason = "Can\U2019t make \"/Volumes/LaCie/Midia/Series/\" into type constant.";
OSAScriptErrorAppAddressKey = "<NSAppleEventDescriptor: [0x0,c00c \"Finder\"]>";
OSAScriptErrorAppNameKey = Finder;
OSAScriptErrorBriefMessageKey = "Can\U2019t make \"/Volumes/LaCie/Midia/Series/\" into type constant.";
OSAScriptErrorExpectedTypeKey = "<NSAppleEventDescriptor: 'enum'>";
OSAScriptErrorMessageKey = "Finder got an error: Can\U2019t make \"/Volumes/LaCie/Midia/Series/\" into type constant.";
OSAScriptErrorNumberKey = "-1700";
OSAScriptErrorOffendingObjectKey = "<NSAppleEventDescriptor: 'utxt'(\"/Volumes/LaCie/Midia/Series/\")>";
OSAScriptErrorRangeKey = "NSRange: {0, 0}";
Any ideas?
Thanks!
EDIT:
So, #lauri helped me with the moving part, the only thing wrong is that the file is copied, not moved, meaning the original file stays in the same place. I want it trashed or copleteley moved. Any ideas?
EDIT 2:
So, I've implemented what #lauri suggested and I still have an error (on the mv command), any ideas?
on hazelProcessFile(theFile)
set text item delimiters to ":"
set filename to last text item of (theFile as text)
set text item delimiters to "."
if filename contains "." then
set base to text items 1 thru -2 of filename as text
set extension to "." & text item -1 of filename
else
set base to filename
set extension to ""
end if
set text item delimiters to {"WEB.DL.DD5.1.H.264.HWD", "WEB-DL.AAC2.0.H.264-NTb", "WEB-DL.DD5.1.H.264-CtrlHD", "WEB.DL.DD5.1.H.264", "PMP", "720p", "HDTV", "x264", "IMMERSE", "-", "E01", "…", "E02", "EVOLVE", "DIMENSION", "E03", "E04", "E05", "E06", "E07", "E08", "E09", "E10", "E11", "E12", "E13", "E14", "E15", "E16", "E17", "E18", "E19", "E20", "E21", "E22", "E23", "E24", "E25", "E26", "E27", "E28", "E29", "E30", "2012", "2013", "2014", "2015", "2016", "FoV", "FOV", "fov", "1080p", "X264", "AFG", "afg", "xvid", "XviD", "XVID", "INTERNAL", "PROPER", "FQM", "fqm", "LOL", "lol", "REWARD", "reward", "WEB", "DL", "AAC2", "H.264", "NTb", "CtrlHD", "DD5", "eztv", "EZTV", "WEB", "VTV", "mSD", "CTU", "hdtv", "evolve", "immerse", "+", "PublicHD", "HWD"}
set ti to text items of base
set text item delimiters to ""
set newbase to ti as text
set newbase to Replace(newbase, "S0", "Season ")
set newbase to Replace(newbase, "S1", "Season 1")
set newbase to Replace(newbase, "S2", "Season 2")
set newbase to Replace(newbase, ".", " ")
set newbase to Replace(newbase, " ", "")
set newbase to Replace(newbase, " ", "")
set newbase to Replace(newbase, " ", "")
set folderLocation to "/Volumes/LaCie/Midia/Series"
set folderName to newbase as text
tell application "Finder"
if newbase contains "Season" then
if not (exists folder (POSIX file "/Volumes/LaCie/Midia/Series/" & newbase as text)) then
-- make new folder at POSIX file "/Volumes/LaCie/Midia/Series/" with properties {name:newbase as text}
-- move theFile to POSIX file "/Volumes/LaCie/Midia/Series/" & newbase as text
do shell script "d=/Volumes/LaCie/Midia/Series/" & quoted form of newbase & "
mkdir -p \"$d\"
mv " & quoted form of POSIX path of theFile & " $d"
else
-- move theFile to POSIX file "/Volumes/LaCie/Midia/Series/" & newbase as text
do shell script "d=/Volumes/LaCie/Midia/Series/" & quoted form of newbase & "
mv " & quoted form of POSIX path of theFile & " $d"
set name of result to newbase
end if
else
-- move theFile to POSIX file "/Volumes/LaCie/Midia/Filmes/"
do shell script "mv " & quoted form of POSIX path of theFile & " /Volumes/LaCie/Midia/Filmes/"
end if
end tell
end hazelProcessFile
on Replace(input, x, y)
set text item delimiters to x
set ti to text items of input
set text item delimiters to y
ti as text
end Replace

Replace path to with POSIX file and use an at specifier for the location:
tell application "Finder"
make new folder at POSIX file "/tmp" with properties {name:"new folder"}
end tell
FWIW, this question was follow-up to Applescript error while trying to move files with hazel.
Apparently the move command copies files when the target is on a different volume. You could use do shell script instead:
if newbase contains "Season" then
do shell script "d=/Volumes/LaCie/Midia/Series/" & quoted form of newbase & "
mkdir -p \"$d\"
mv " & quoted form of POSIX path of theFile & " $d"
else
do shell script "mv " & quoted form of POSIX path of theFile & " /Volumes/LaCie/Midia/Filmes/"
end if

Related

Applescript - Creating folders based on the first word of the filename

Essentially i'm looking for an applescript that allow me to order the massive 50.000 files by creating folders that have just the first word of the files, ignoring the rest of the filename after the first space.
For eaxmple the 50.000 files are named like this:
- amazingfrog -shootingbase.jpg
- frog 2sHDn1_9fFs12s.jpg
- frog 29adjjdd39939.mov
- Horse IUS39aosdja.mov
- horse 282131888.jpg
- HORSE.jpg
And so on.....
- I would like to be like this:
- amazingfrog
-amazingfrog -shootingbase.jpg
- frog
-frog 2sHDn1_9fFs12s.jpg
-frog 29adjjdd39939.mov
- horse
-horse IUS39aosdja.mov
-horse 282131888.jpg
-horse.gif
And so on....
On the internet i came across with the following script:
set chosenFolder to (choose folder)
tell application "Finder" to set fileList to files of chosenFolder
repeat with aFile in fileList
set {name:Nm, name extension:Ex} to info for (aFile as alias)
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 dateFolder to text 1 thru 15 of Nm
set sourceFile to quoted form of POSIX path of (aFile as text)
set destinationFile to quoted form of (POSIX path of chosenFolder & dateFolder & "/" & name of aFile)
do shell script "ditto " & sourceFile & space & destinationFile
do shell script "rm " & sourceFile
end repeat
The only problem is that i have to choose in the "text 1 thru" the numbers of the letters i want to keep. And unfortunately the first word of the filenames have different length...
Could be possible to modify this script to my needed? or do you have any other suggestions?
Thanks in advance for any reply!!
I recommend to use text item delimiters to extract the first part of the file name
set chosenFolder to (choose folder)
tell application "Finder" to set fileList to files of chosenFolder
set TID to text item delimiters
set text item delimiters to space
repeat with aFile in fileList
set fileName to name of aFile
set textItems to text items of fileName
if (count textItems) = 1 then
set fileExtension to name extension of aFile
set folderName to text 1 thru ((get offset of "." & fileExtension in fileName) - 1) of fileName
else
set folderName to first item of textItems
end if
set sourceFile to quoted form of POSIX path of (aFile as text)
set destinationFile to quoted form of (POSIX path of chosenFolder & folderName & "/" & fileName)
do shell script "ditto " & sourceFile & space & destinationFile
do shell script "rm " & sourceFile
end repeat
set text item delimiters to TID

Applescript to copy file from Google Team Drive

I am trying to copy a folder on a Google Team Drive to another location, but my script keeps getting an error. The full script is below.
I basically want to copy an Excel file "Google Drive:Team Drives:Company -Sync Folder:0000-1_E.xlsx" to a location that is defined by the script.
The offending line is:
duplicate file "Google Drive:Team Drives:Company -Sync Folder:0000-1_E.xlsx" of startup disk to folder (jobNum & " Documents") of newJobFolder
Any help?
global jobNum
global newJobFolder
set jobNum to text returned of (display dialog "Enter a job number:" default answer "")
set jobName to text returned of (display dialog "Enter a job name:" default answer "")
set jobMgr to text returned of (display dialog "Enter account manager email:" default answer "")
set folderpath to (choose folder with prompt "Select client folder")
set newJobFolder to my newFold(jobNum, jobName, folderpath, jobMgr)
on newFold(theNumber, theName, thefolder, jobMgr)
set emailAddress to jobMgr
set emailSubject to theNumber & " -" & theName
set bodyText to ""
set emailLinkFileName to theNumber & " -" & theName
set subNameList to {"Designs", "Documents", "Received"}
set itemCount to count of subNameList
set linkBody to "mailto:" & emailAddress & "?subject=" & my replace_chars(emailSubject, " ", "%20") & "&body=" & my replace_chars(bodyText, " ", "%20")
tell application "Finder"
set newJobFolder to (make new folder at thefolder with properties ¬
{name:theNumber & " " & theName})
repeat with i from 1 to itemCount
set aSubFolder to make new folder at newJobFolder with properties {name:jobNum & " " & item i of subNameList}
set theMailto to make new internet location file to linkBody at aSubFolder with properties {name:emailLinkFileName, name extension:"mailloc"}
set the name extension of theMailto to "mailloc"
end repeat
duplicate file "Google Drive:Team Drives:Company -Sync Folder:0000-1_E.xlsx" of startup disk to folder (jobNum & " Documents") of newJobFolder
set name of the result to jobNum & " E.xlsx"
set theMailto to make new internet location file to linkBody at newJobFolder with properties {name:emailLinkFileName, name extension:"mailloc"}
set the name extension of theMailto to "mailloc"
end tell
end newFold
-- Generic find and replace functionality
on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
Assuming that your "Google Drive" folder resides directly in your home folder...
Try changing this...
duplicate file "Google Drive:Team Drives:Company -Sync Folder:0000-1_E.xlsx" of startup disk to folder (jobNum & " Documents") of newJobFolder
To this...
set theFile to (path to home folder as text) & "Google Drive:Team Drives:Company -Sync Folder:0000-1_E.xlsx"
duplicate file theFile to folder (jobNum & " Documents") of newJobFolder
And if that does not work, You can try this approach...
set theFile to (choose file) as alias as text
duplicate file theFile to folder (jobNum & " Documents") of newJobFolder
Then after you run it once, comment out the choose file command.
If you're using Google Drive File Stream, which is the one for businesses, the mounted drive can be reached through the user folder, the path is something like this:
"/Users/nameofuser/Google Drive File Stream/My Drive/Companyfolder"
Use this format to get to the folders and it should work, that's what I do.

Applescript to transfer spotlight comment and label

Am writing Applescript to take a list of folders, compress them to .zip files, and transfer the spotlight comment and label from the folder to the new file.
Thank you to CRGreen for the suggestion. Here is the final script.
on run {input, parameters}
tell application "Finder"
set theItems to selection
repeat with i from 1 to (count of theItems)
set theItem to (item i of theItems) as alias
set itemPath to quoted form of POSIX path of theItem
set theParent to POSIX path of (container of theItem as alias)
set fileName to theParent & (name of theItem) & ".zip"
set zipFile to quoted form of fileName
do shell script "zip -jr " & zipFile & " " & itemPath
do shell script "setfile -a E " & zipFile
set newItem to POSIX file fileName as alias
set comment of newItem to (get comment of theItem)
set label index of newItem to (get label index of theItem)
set oldFolder to quoted form of (theParent & name of theItem)
do shell script "rm -rf " & oldFolder
end repeat
end tell
return input
end run
The magic of parentheses! (and alias coercion):
set comment of ((POSIX file newItem) as alias) to theComment
set label index of ((POSIX file newItem) as alias) to theLabel

Moving movies and series with hazel, Applescript error

So, I've got this:
on hazelProcessFile(theFile)
set text item delimiters to ":"
set filename to last text item of (theFile as text)
set text item delimiters to "."
if filename contains "." then
set base to text items 1 thru -2 of filename as text
set extension to "." & text item -1 of filename
else
set base to filename
set extension to ""
end if
set text item delimiters to {"WEB.DL.DD5.1.H.264.HWD", "WEB-DL.AAC2.0.H.264-NTb", "WEB-DL.DD5.1.H.264-CtrlHD", "WEB.DL.DD5.1.H.264", "PMP", "720p", "HDTV", "x264", "IMMERSE", "-", "E01", "…", "E02", "EVOLVE", "DIMENSION", "E03", "E04", "E05", "E06", "E07", "E08", "E09", "E10", "E11", "E12", "E13", "E14", "E15", "E16", "E17", "E18", "E19", "E20", "E21", "E22", "E23", "E24", "E25", "E26", "E27", "E28", "E29", "E30", "2012", "2013", "2014", "2015", "2016", "FoV", "FOV", "fov", "1080p", "X264", "AFG", "afg", "xvid", "XviD", "XVID", "INTERNAL", "PROPER", "FQM", "fqm", "LOL", "lol", "REWARD", "reward", "WEB", "DL", "AAC2", "H.264", "NTb", "CtrlHD", "DD5", "eztv", "EZTV", "WEB", "VTV", "mSD", "CTU", "hdtv", "evolve", "immerse", "+", "PublicHD", "HWD"}
set ti to text items of base
set text item delimiters to ""
set newbase to ti as text
set newbase to Replace(newbase, "S0", "Season ")
set newbase to Replace(newbase, "S1", "Season 1")
set newbase to Replace(newbase, "S2", "Season 2")
set newbase to Replace(newbase, ".", " ")
set newbase to Replace(newbase, " ", "")
set newbase to Replace(newbase, " ", "")
set newbase to Replace(newbase, " ", "")
set folderLocation to "/Volumes/LaCie/Midia/Series"
set folderName to newbase as text
tell application "Finder"
if newbase contains "Season" then
if not (exists folder (POSIX file "/Volumes/LaCie/Midia/Series/" & newbase as text)) then
-- make new folder at POSIX file "/Volumes/LaCie/Midia/Series/" with properties {name:newbase as text}
-- move theFile to POSIX file "/Volumes/LaCie/Midia/Series/" & newbase as text
do shell script "d=/Volumes/LaCie/Midia/Series/" & quoted form of newbase & "
mkdir -p \"$d\"
mv " & quoted form of POSIX path of theFile & " $d"
else
-- move theFile to POSIX file "/Volumes/LaCie/Midia/Series/" & newbase as text
do shell script "d=/Volumes/LaCie/Midia/Series/" & quoted form of newbase & "
mv " & quoted form of POSIX path of theFile & " $d"
set name of result to newbase
end if
else
-- move theFile to POSIX file "/Volumes/LaCie/Midia/Filmes/"
do shell script "mv " & quoted form of POSIX path of theFile & " /Volumes/LaCie/Midia/Filmes/"
end if
end tell
end hazelProcessFile
on Replace(input, x, y)
set text item delimiters to x
set ti to text items of input
set text item delimiters to y
ti as text
end Replace
I'm getting an error specifically in the part where it is suposed to create a folder and move the file (mv command).
The script is activated from a hazel rule and it's supposed to discover if a file is a movie or a TV show episode (cleaning the file name), and move it to the respective folder (if it doesn't exists, it creates the folder)
Any ideas?
Thanks!
Try:
set d to quoted form of ("/Volumes/LaCie/Midia/Series/" & newbase)
do shell script "mkdir -p " & d & " ; mv " & quoted form of (POSIX path of theFile) & space & d

How to get the Color profile of an Image using Applescript?

)
I want to write an apple-script which collect the color profile of an image.. can anyone please help me out how to do this? I've no idea!!
Thanks in advance.
I like using ExifTool by Phil Harvey to extract metadata. Here is a service I wrote to access the metadata quickly.
on run {input, parameters}
-- creates a metadata folder in the Documents folder to store results
set thePath to POSIX path of (path to documents folder) & "metadata" & "/"
do shell script "mkdir -p " & quoted form of POSIX path of thePath
set {inputFiles, outputFiles} to {{}, {}}
repeat with anItem in input
set end of inputFiles to quoted form of POSIX path of (anItem as text)
tell application "Finder" to set {name:fileName, name extension:nameExtension} to anItem
set baseName to text 1 thru ((get offset of "." & nameExtension in fileName) - 1) of fileName
set end of outputFiles to quoted form of (thePath & baseName & ".txt")
end repeat
set {TID, text item delimiters} to {text item delimiters, space}
set {inputFiles, outputFiles} to {(inputFiles as text), (outputFiles as text)}
set text item delimiters to TID
do shell script "exiftool -a " & inputFiles & " -w " & quoted form of (thePath & "%f.txt" as text) & "; open " & outputFiles
end run

Resources