AppleScript Droplet to Convert PSD and TIF to JPG - applescript

There are a lot of examples of converters to JPG and i am trying to change one for my needs but i need little help please.
The requirements are:
It should be an AppleScript Droplet. I am using Script Editor ( for some reason Automator can't run a simple droplet drag and drop function for me ).
The output folder for the JPGs should not be prompted by the user .. but set as a variable in the code permanently and easly changed.
The quality ( compression ) of the converted JPG should also have to be easly customisable in the code.
The converted JPG files have to be converted to Color profile Adobe RGB 1998 if necessary.
I know Image Events allow us to set the JPG compression like :
save openedFile as JPEG with compression level (low|medium|high)
but unfortunately i need more customisation.
A shell script will help me to set the level from 10 to 100 but unfortunately i can't implement the shell script properly.
Little help please about points 3 and 4.
Thank you !
on run
display dialog "Please drag image files to this script to turn them into JPEGs"
end run
on open draggeditems
set End_Folder to "Macintosh HD:Users:zzz:Desktop:End"
repeat with currentFile in draggeditems
tell application "Image Events"
set openedFile to open (currentFile as alias)
set fileLocation to the location of openedFile
set fileName to the name of openedFile
set Path_to_Converted_File to (End_Folder & ":" & text 1 thru -5 of fileName & ".jpg")
do shell script "sips --setProperty formatOptions 10 " & openedFile
save openedFile as JPEG in Path_to_Converted_File
--save openedFile as JPEG with compression level low in Path_to_Converted_File (low|medium|high)
close openedFile
end tell
end repeat
end open

Mixing up Image Events and sips is more confusing than useful, and since sips can perform the various options you are looking for (such as 3 & 4), it makes more sense to use it for everything. Setting a few variables for the various options will let you change them as needed, or if adding preferences or whatever. The sips man page will give you more details about the various options; I’ve added comments for the ones used in the following script:
on run
open (choose file with prompt "Select image files to turn into JPEGs:" with multiple selections allowed)
end run
on open draggeditems
set destination to (((path to desktop folder) as text) & "End:") -- folder path (trailing delimiter)
set format to "jpeg" -- jpeg | tiff | png | gif | jp2 | pict | bmp | qtif | psd | sgi | tga
set extension to ".jpg" -- extension to match format
set compression to 10 -- low | normal | high | best | <percent>
set profile to quoted form of "/System/Library/ColorSync/Profiles/AdobeRGB1998.icc" -- POSIX path to color profile
repeat with thisFile in draggeditems
set theName to justTheName for thisFile
set originalFile to quoted form of POSIX path of thisFile
set convertedFile to quoted form of POSIX path of (destination & theName & extension)
do shell script "sips -s format " & format & " -s formatOptions " & compression & " -m " & profile & space & originalFile & " --out " & convertedFile
end repeat
end open
on justTheName for filePath
tell application "System Events" to tell disk item (filePath as text)
set {fileName, extension} to {name, name extension}
end tell
if extension is not "" then set fileName to text 1 thru -((count extension) + 2) of fileName -- just the name part
return fileName
end justTheName
Edited to add:
The shell script is expecting POSIX paths, so the aliases passed to the open handler are coerced and quoted in the event they contain spaces.

There is a lot wrong in your script. It is not clear why you are opening the file for writing before sending it to a shell command. sips does not operate on open FILE references. sips will need to open the file from a POSIX path. I think this may do what you want (you will need to implement error checking, etc.):
on open draggeditems
set End_Folder to "~/Desktop/End/"
repeat with currentFile in draggeditems
do shell script "sips --setProperty formatOptions 10 " & quoted form of POSIX path of currentFile & " --out " & End_Folder
end repeat
end open

Related

Applescript: can't make file "xx" into type <<class fsrf>>

I'm trying to get a script working which is able to batch export .mts format video files via quicktime into .mov files in 1080p. The script fails with the following error: "The action “Run AppleScript” encountered an error: “QuickTime Player got an error: Can’t make file (document "00000.MTS") into type «class fsrf».”". I assume this has something to do with using text file paths? Note I'm not experienced with Applescript and would really appreciate any help to get this simple bit of script working. Currently it's in automator as a service:
on run {inputFiles}
if inputFiles is equal to {} then
set inputFiles to (choose file with prompt "Select the file(s) to convert:" with multiple selections allowed without invisibles)
end if
open inputFiles
end run
on open droppedItems
tell application "Finder" to set inputFolder to (container of first item of droppedItems) as Unicode text
set outputFolder to (choose folder with prompt "Select output folder:" default location (inputFolder as alias)) as Unicode text
set exportPreset to (choose from list {"Movie", "iPhone", "iPod", "480p", "720p", "1080p"} with prompt "Choose QuickTime Export Preset:") as Unicode text
if exportPreset is equal to "false" then
return
end if
repeat with currentItem in droppedItems
repeat until getProcessPercentCPU("CoreMediaAuthoringSessionHelper") is equal to ""
end repeat
tell application "Finder" to set fileName to name of currentItem as Unicode text
set fileName to text 1 thru ((fileName's length) - (offset of "." in ¬
(the reverse of every character of fileName) as text)) of fileName
convertFile(currentItem, outputFolder & fileName & ".mov", exportPreset)
end repeat
end open
on convertFile(inputFile, outputFile, exportPreset)
tell application "QuickTime Player"
set thisMovie to open inputFile
open for access file thisMovie
close access file thisMovie
export thisMovie in (outputFile) using settings preset exportPreset
close thisMovie
end tell
end convertFile
on getProcessPercentCPU(processName)
do shell script "/bin/ps -xco %cpu,command | /usr/bin/awk '/" & processName & "$/ {print $1}'"
end getProcessPercentCPU
Try changing:
set outputFolder to (choose folder with prompt "Select output folder:" default location (inputFolder as alias)) as Unicode text
to:
set outputFolder to POSIX path of (choose folder with prompt "Select output folder:" default location (inputFolder as alias))
and:
convertFile(currentItem, outputFolder & fileName & ".mov", exportPreset)
to:
set outputFile to POSIX file (outputFolder & fileName & ".mov")
convertFile(currentItem, outputFile, exportPreset)
and remove the open for access/close access commands.
Sandboxed apps don't like receiving path strings to open/save commands, but accept alias/POSIX file values okay. (If it still doesn't work then it's some other issue at play, but that's always the first thing to check when you get a filesystem permissions error as you describe.)
The error occurs because thisMovie is a document reference of QuickTime Player and this class cannot be converted / coerced to a file system reference («class fsrf»).
That's what the error message says
Can’t make file (document "00000.MTS") into type «class fsrf»
The Standard Additions command open for access does not support QuickTime Player documents anyway. What is the purpose of the open / close lines?
Note:
as Unicode text as coercion to string is outdated since macOS 10.5 Leopard. It's only used with read and write commands to handle UTF16 encoded text. A coercion to standard AppleScript text is simply written as text. In case of name of currentItem it's redundant anyway because the class of name is always text.

Syntax Error for expected end of line bunt found unknown token writing Apple script for folder actions

For the life of me I can't figure out what's causing this error. I have made sure to disable smart quotes, but keep getting the same error. The script itself is intended to automate handbrake video conversion, and admittedly I'm very new to playing with apple script but can't figure out what's causing this. Any help would be greatly appreciated.
Here is the full code in case it helps:
on adding folder items to this_folder after receiving these_items
with timeout of (720 * 60) seconds
tell application "Finder"
--Get all MOV files that have no label color yet, meaning it hasn't been processed
set allFiles to every file of entire contents of ("Macintosh HD:Users:MacMini:Downloads:Downloaded" as alias) whose (name extension is "mov" and label index is 0)
--Repeat for all files in above folder
repeat with i from 1 to number of items in allFiles
set currentFile to (item i of allFiles)
try
--Set to gray label to indicate processing
set label index of currentFile to 7
--Assemble original and new file paths
set origFilepath to quoted form of POSIX path of (currentFile as alias)
set newFilepath to (characters 1 thru -5 of origFilepath as string) & "mp4'"
--Start the conversion
set shellCommand to "nice /Applications/HandBrakeCLI -i " & origFilepath & " -o " & newFilepath & " --preset=\"Fast 720p30\" --optimize ;"
tell current application
do shell script shellCommand
end tell
--Set the label to green in case file deletion fails
set label index of currentFile to 6
--Remove the old file
set shellCommand to "rm -f " & origFilepath
tell current application
do shell script shellCommand
end tell
on error errmsg
--Set the label to red to indicate failure
set label index of currentFile to 2
end try
end repeat
end tell
end timeout
end adding folder items to
It's a typical copy & paste error:
Replace all occurrences of & with &

Resize images to specific width only (AppleScript or Automator)

I've been searching google.com a couple of hours trying to find a way to do this, but there is no answer. Everywhere is resize to the longest size, but nothing about resize to the specified width only.
Is there any way in Automator or AppleScript to resize images to a specified width only, instead of just the longest size? I need my output images to be a specific width ony (e.g 200px).
You can do this with plain AppleScript and a shell utility entitled sips:
on open droppings
repeat with everyDrop in droppings
set originalFile to quoted form of POSIX path of (everyDrop as text)
tell application "Finder"
set originalName to everyDrop's name
set imageContainer to (everyDrop's container as text)
end tell
set reSizedName to "200W" & originalName
set outputPath to quoted form of POSIX path of (imageContainer & reSizedName)
do shell script "sips --resampleWidth 200 " & originalFile & " --out " & outputPath
end repeat
end open
on run
display dialog "Drop some Image Files to Re-size them all to 200 pixels wide" buttons {"Aye Aye"} default button "Aye Aye"
end run
This preserves the aspect ratio of the original image, and simply re-sizes the width to 200 pixels. Hopefully you can see where you can make the changes necessary for your own workflow.
If you want to drop a folder of images as well as individual files, try this as a droplet:
on open droppings
repeat with everyDrop in droppings
if (info for everyDrop)'s folder is true then
tell application "Finder" to set allImageFiles to everyDrop's every file
repeat with eachFile in allImageFiles
my SetWidthTo200(eachFile)
end repeat
else
my SetWidthTo200(everyDrop)
end if
end repeat
end open
to SetWidthTo200(img)
set originalFile to quoted form of POSIX path of (img as text)
tell application "Finder"
set originalName to img's name
set imageContainer to (img's container as text)
end tell
set reSizedName to "200W" & originalName
set outputPath to quoted form of POSIX path of (imageContainer & reSizedName)
do shell script "sips --resampleWidth 200 " & originalFile & " --out " & outputPath
end SetWidthTo200
on run
display dialog "Drop some Image Files to Re-size them all to 200 pixels wide" buttons {"Aye Aye"} default button "Aye Aye"
end run
There is still no error-checking or checking to be sure that the files are indeed image files, so keep that in mind.
Here's a summary from this OSX Tips using automator to resize images
open "Automator"
File > New > Service
Settings:
Service received selected: "image files"
in "Finder"
in the left pane, find for action named "Get Selected Finder Items"
Next look for action "Scale Images"
If you wish to replace original, choose "Don’t Add" when it ask
"would you like to add a Copy Finder Items action", otherwise, choose add
In order to be able to choose the highest pixels before scaling, click on Options at the right of results, tick
"show this action when the workflow run"

FileZilla and applescript

I am making an applescript that converts a folder of flv and f4v files to mp4 files and then uploads the to a server via filezilla. How would I use applescript to upload to a server through Filezilla? Here is my code:
--Install handbrakecli into /usr/bin/
--on adding folder items to this_folder after receiving these_items
with timeout of (720 * 60) seconds
tell application "Finder"
--Get all flv and f4v files that have no label color yet, meaning it hasn't been processed
set allFiles to every file of entire contents of ("Macintosh HD:Users:Chase:auto_convert:nope" as alias) whose ((name extension is "flv" or name extension is "f4v") and label index is 0)
--Repeat for all files in above folder
repeat with i from 1 to number of items in allFiles
set currentFile to (item i of allFiles)
try
--label to indicate processing
set label index of currentFile to 3
--Assemble original and new file paths
set origFilepath to quoted form of POSIX path of (currentFile as alias)
set newFilepath to (characters 1 thru -5 of origFilepath as string) & "mp4'"
--Start the conversion
tell application "Terminal"
do shell script "HandBrakeCLI -i " & origFilepath & " -o " & newFilepath
end tell
--Set the label to red because this is the file that has been converted
set label index of currentFile to 6
--Remove the old file
on error errmsg
--Set the label to red to indicate failure
set label index of currentFile to 2
end try
end repeat
set extensionToFind to "mp4"
set topLevelFolder to "Macintosh HD:Users:Chase:auto_convert:nope" as text
set pathCount to count of topLevelFolder
set mp4Files to files of entire contents of folder topLevelFolder whose name extension is extensionToFind
if mp4Files is {} then return
set mp4Folder to "Macintosh HD:Users:Chase:auto_convert:yep"
move mp4Files to mp4Folder
end tell
end timeout
--end adding folder items to
Not a good idea, because Filezilla has no applescript support. I've always been surprised that Cyberduck doesn't either. But see:
http://discussions.apple.com/thread/2588937?start=0&tstart=0
.... on which there are good directions; curl in shell, or (at the end of the page) URL Access Scripting, which is a scripting addition that should be installed on your Mac. URL Access Scripting example:
tell application "URL Access Scripting"
upload filepathtoUpload to "ftp://username:password#domain.com/SOME/PATH/filename.jpg" replacing yes without binhexing
end tell

Use Automator/Applescript to crop filenames after certain character?

I have a folder containing about 5000 files with names like:
Invoice 10.1 (2012) (Digital) (4-Attachments).pdf
Carbon Copy - Invoice No 02 (2010) (2 Copies) (Filed).pdf
01.Reciept #04 (Scanned-Copy).doc
I want to rename these files by removing everything from the first bracket onwards, so they look like this:
Invoice 10.1.pdf
Carbon Copy - Invoice No 02.pdf
01.Reciept #04.doc
I have found lots of scripts that will remove the last x letters, but nothing that will crop from a particular character.
Ideally I would like to use Automator, but I'm guess this might too complex for it. Any ideas?
Try:
set xxx to (choose folder)
tell application "Finder"
set yyy to every paragraph of (do shell script "ls " & POSIX path of xxx)
repeat with i from 1 to count of yyy
set theName to item i of yyy
set name of (file theName of xxx) to (do shell script "echo " & quoted form of theName & " | sed s'/ (.*)//'")
end repeat
end tell
The code posted by #adayzone will work, but there is no need to use sed for this – plain AppleScript will do, using offset:
set fullString to "Invoice 10.1 (2012) (Digital) (4-Attachments).pdf"
set trimmedString to text 1 thru ((offset of "(" in fullString) - 1) of fullString
-- trim trailing spaces
repeat while trimmedString ends with " "
set trimmedString to text 1 thru -2 of trimmedString
end repeat
this returns “Invoice 10.1". To split the file name into the name and extension, and re-add the extension, you can use System Events’ Disk-File-Folder suite, which will provide the handy name extension property you can store and re-add after trimming the name.
Assuming you use some Automator action to get the files to be processed, the full processing workflow would be to add an AppleScript action after the file selection part with the following code:
repeat with theFile in (input as list)
tell application "System Events"
set theFileAsDiskItem to disk item ((theFile as alias) as text)
set theFileExtension to name extension of theFileAsDiskItem
set fullString to name of theFileAsDiskItem
-- <insert code shown above here>
set name of theFileAsDiskItem to trimmedString & "." & theFileExtension
end tell
end repeat
If you want your Automator workflow to process the files any further, you will also have to create a list of aliases to the renamed files and return that from the AppleScript action (instead of input, which, of course, is not valid anymore).

Resources