CreateLowRes: Difference between revisions

From Wurst-Wasser.net
Jump to navigation Jump to search
No edit summary
No edit summary
 
(8 intermediate revisions by 2 users not shown)
Line 1: Line 1:
This AppleScript automagically creates low res images from high res images in a hot folder.
This [[AppleScript]] automagically creates low res images from high res images in a hot folder.


Please don't complain about formatting - it's hard enough to make this look good in [[UseMod]] Wiki.  
Please don't complain about formatting - it's hard enough to make this look good in a Wiki.


Download here: upload:createlowres2009-06-30.zip
Download here: [[File:createlowres2009-06-30.zip]]


<tt>
<tt>
:(* CreateLowRes.scpt (c) by Heiko Kretschmer, heiko@wurst-wasser.net *)
:<nowiki>(* CreateLowRes.scpt (c) by Heiko Kretschmer *)</nowiki>
::
::<nowiki></nowiki>
:(* *************************************************************************** *)
:<nowiki>(* *************************************************************************** *)</nowiki>
:(* Globals *)
:<nowiki>(* Globals *)</nowiki>
:<nowiki>global gTargetDPI, gLocked, gFolderNameLowRes, gSendReturnMax, gDebugLog, gSearchFolder, gFileLogPOSIXPath, gFileSuffixesToProcess</nowiki>
:<nowiki>global gTargetDPI, gLocked, gFolderNameLowRes, gSendReturnMax, gDebugLog, gSearchFolder, gFileLogPOSIXPath, gFileSuffixesToProcess</nowiki>
:<nowiki>global gTargetSuffix</nowiki>
:<nowiki>global gTargetSuffix</nowiki>
::
::<nowiki></nowiki>
:<nowiki>#, gSearchPath</nowiki>
:<nowiki>#, gSearchPath</nowiki>
:# property new_foldername : "TIFF Images"
:<nowiki># property new_foldername : "TIFF Images"</nowiki>
::
::<nowiki></nowiki>
:(* *************************************************************************** *)
:<nowiki>(* *************************************************************************** *)</nowiki>
:(* Functions *)
:<nowiki>(* Functions *)</nowiki>
:on main(scanFolderMacOS)
:<nowiki>on main(scanFolderMacOS)</nowiki>
::<nowiki>if gLocked is false then</nowiki>
::<nowiki>if gLocked is false then</nowiki>
:::<nowiki>set gLocked to true</nowiki>
:::<nowiki>set gLocked to true</nowiki>
:::<nowiki>tell me to say ("Starting up, checking " & (POSIX path of scanFolderMacOS) as string)</nowiki>
:::<nowiki>tell me to say ("Starting up, checking " & (POSIX path of scanFolderMacOS) as string)</nowiki>
:
:<nowiki></nowiki>
:::(* Check this folder and its sub-folders *)
:::<nowiki>(* Check this folder and its sub-folders *)</nowiki>
:::<nowiki>scanFolder(scanFolderMacOS)</nowiki>
:::<nowiki>scanFolder(scanFolderMacOS)</nowiki>
:
:<nowiki></nowiki>
:::<nowiki>set gLocked to false (* We're through, unlock *)</nowiki>
:::<nowiki>set gLocked to false (* We're through, unlock *)</nowiki>
::else # if locked
::<nowiki>else # if locked</nowiki>
:::tell me to say ("We're locked, so I'm skipping this run.")
:::<nowiki>tell me to say ("We're locked, so I'm skipping this run.")</nowiki>
::end if (* not locked *)
::<nowiki>end if (* not locked *)</nowiki>
:end main
:<nowiki>end main</nowiki>
::
::<nowiki></nowiki>
:(* create low res file, if low res does not exist, or low res is older than highres *)
:<nowiki>(* create low res file, if low res does not exist, or low res is older than highres *)</nowiki>
:on createLowResOfFile(fileHighRes)
:<nowiki>on createLowResOfFile(fileHighRes)</nowiki>
:::
:::<nowiki></nowiki>
::(* Create LowRes-Folder *)
::<nowiki>(* Create LowRes-Folder *)</nowiki>
::set fileHighResPOSIXPath to (POSIX path of fileHighRes)
::<nowiki>set fileHighResPOSIXPath to (POSIX path of fileHighRes)</nowiki>
::tell me to say ("fileHighResPOSIXPath: " & fileHighResPOSIXPath)
::<nowiki>tell me to say ("fileHighResPOSIXPath: " & fileHighResPOSIXPath)</nowiki>
::tell me to set containingFolder to pathOfPOSIXFilepath(fileHighResPOSIXPath) & "/"
::<nowiki>tell me to set containingFolder to pathOfPOSIXFilepath(fileHighResPOSIXPath) & "/"</nowiki>
::set containingFolderAlias to POSIX file containingFolder
::<nowiki>set containingFolderAlias to POSIX file containingFolder</nowiki>
::tell me to say ("Containing folder: \"" & containingFolder & "\"")
::<nowiki>tell me to say ("Containing folder: \"" & containingFolder & "\"")</nowiki>
::tell application "Finder"
::<nowiki>tell application "Finder"</nowiki>
:::if not (the folder gFolderNameLowRes of folder containingFolderAlias exists) then (* http://applescript.codefetch.com/example/k5/Applescript%20Examples%20CD(converted)/Chapter%205/Page%2090%20-%20Creating%20a%20Folder%20(Only%20if%20Necessary).scpt.txt?qy=if *)
:::<nowiki>if not (the folder gFolderNameLowRes of folder containingFolderAlias exists) then (* http://applescript.codefetch.com/example/k5/Applescript%20Examples%20CD(converted)/Chapter%205/Page%2090%20-%20Creating%20a%20Folder%20(Only%20if%20Necessary).scpt.txt?qy=if *)</nowiki>
::::tell me to say ("Creating low-res-folder \"" & gFolderNameLowRes & "\" in \"" & containingFolder & "\"")
::::<nowiki>tell me to say ("Creating low-res-folder \"" & gFolderNameLowRes & "\" in \"" & containingFolder & "\"")</nowiki>
::::make new folder at containingFolderAlias with properties {name:gFolderNameLowRes} (* http://www.macresearch.org/tutorial_applescript_for_scientists_part_i *)
::::<nowiki>make new folder at containingFolderAlias with properties {name:gFolderNameLowRes} (* http://www.macresearch.org/tutorial_applescript_for_scientists_part_i *)</nowiki>
:::end if
:::<nowiki>end if</nowiki>
::end tell
::<nowiki>end tell</nowiki>
::tell me to set fileName to fileOfPOSIXFilepath(fileHighResPOSIXPath)
::<nowiki>tell me to set fileName to fileOfPOSIXFilepath(fileHighResPOSIXPath)</nowiki>
::tell me to set filenameWithoutSuffix to filenameWithoutSuffix(fileName)
::<nowiki>tell me to set filenameWithoutSuffix to filenameWithoutSuffix(fileName)</nowiki>
::tell me to say ("filenameWithoutSuffix: " & filenameWithoutSuffix)
::<nowiki>tell me to say ("filenameWithoutSuffix: " & filenameWithoutSuffix)</nowiki>
::set lowResFileNameWithoutSuffix to filenameWithoutSuffix (* In our case, the filename of the low-res-image is the same as the original file (except for the suffix) *)
::<nowiki>set lowResFileNameWithoutSuffix to filenameWithoutSuffix (* In our case, the filename of the low-res-image is the same as the original file (except for the suffix) *)</nowiki>
::tell me to say ("lowResFileNameWithoutSuffix: " & lowResFileNameWithoutSuffix)
::<nowiki>tell me to say ("lowResFileNameWithoutSuffix: " & lowResFileNameWithoutSuffix)</nowiki>
::set lowResFileName to lowResFileNameWithoutSuffix & "." & gTargetSuffix
::<nowiki>set lowResFileName to lowResFileNameWithoutSuffix & "." & gTargetSuffix</nowiki>
::tell me to say ("lowResFileName: " & lowResFileName)
::<nowiki>tell me to say ("lowResFileName: " & lowResFileName)</nowiki>
::set lowResFolderAlias to POSIX file (containingFolder & gFolderNameLowRes & "/")
::<nowiki>set lowResFolderAlias to POSIX file (containingFolder & gFolderNameLowRes & "/")</nowiki>
::tell me to say ("lowResFolderAlias: " & lowResFolderAlias)
::<nowiki>tell me to say ("lowResFolderAlias: " & lowResFolderAlias)</nowiki>
::set lowResFilePOSIXPath to (POSIX path of lowResFolderAlias) & lowResFileName
::<nowiki>set lowResFilePOSIXPath to (POSIX path of lowResFolderAlias) & lowResFileName</nowiki>
::tell me to say ("lowResFilePOSIXPath: " & lowResFilePOSIXPath)
::<nowiki>tell me to say ("lowResFilePOSIXPath: " & lowResFilePOSIXPath)</nowiki>
:::
:::<nowiki></nowiki>
::(* Check for Low Res file / compare age *)
::<nowiki>(* Check for Low Res file / compare age *)</nowiki>
::(* First, check if LowRes-File exists *)
::<nowiki>(* First, check if LowRes-File exists *)</nowiki>
::tell application "Finder" to set lowResFileExists to (the file lowResFileName of folder lowResFolderAlias exists)
::<nowiki>tell application "Finder" to set lowResFileExists to (the file lowResFileName of folder lowResFolderAlias exists)</nowiki>
::tell me to say ("lowResFileExists: " & lowResFileExists)
::<nowiki>tell me to say ("lowResFileExists: " & lowResFileExists)</nowiki>
:::
:::<nowiki></nowiki>
::(* Compare file ages *)
::<nowiki>(* Compare file ages *)</nowiki>
::# Some bad property, since its language specific: modification date:date "Samstag, 27. Juni 2009 16:36:49 Uhr"
::<nowiki># Some bad property, since its language specific: modification date:date "Samstag, 27. Juni 2009 16:36:49 Uhr"</nowiki>
::if (lowResFileExists) then
::<nowiki>if (lowResFileExists) then</nowiki>
:::(* if the low res file already exists, compare file age with original file *)
:::<nowiki>(* if the low res file already exists, compare file age with original file *)</nowiki>
:::tell me to set ageOfLowRes to ageOfFile(lowResFilePOSIXPath)
:::<nowiki>tell me to set ageOfLowRes to ageOfFile(lowResFilePOSIXPath)</nowiki>
:::tell me to set ageOfHiRes to ageOfFile(fileHighResPOSIXPath)
:::<nowiki>tell me to set ageOfHiRes to ageOfFile(fileHighResPOSIXPath)</nowiki>
:::tell me to say ("ageOfHiRes: " & ageOfHiRes)
:::<nowiki>tell me to say ("ageOfHiRes: " & ageOfHiRes)</nowiki>
:::tell me to say ("ageOfLowRes: " & ageOfLowRes)
:::<nowiki>tell me to say ("ageOfLowRes: " & ageOfLowRes)</nowiki>
:::if (ageOfLowRes is greater than ageOfHiRes) then
:::<nowiki>if (ageOfLowRes is greater than ageOfHiRes) then</nowiki>
::::tell me to say ("Skipping file \"" & fileHighResPOSIXPath & "\", because its low res version has already been created.")
::::<nowiki>tell me to say ("Skipping file \"" & fileHighResPOSIXPath & "\", because its low res version has already been created.")</nowiki>
::::return
::::<nowiki>return</nowiki>
:::end if
:::<nowiki>end if</nowiki>
::end if
::<nowiki>end if</nowiki>
:
:<nowiki> </nowiki>
::(* stop here while debugging  *)
::<nowiki>(* stop here while debugging  *)</nowiki>
::# return
::<nowiki># return</nowiki>
:
:<nowiki></nowiki>
::(* Create Low Res *)
::<nowiki>(* Create Low Res *)</nowiki>
::tell me to say ("Creating low-res version of \"" & fileHighResPOSIXPath & "\"...")
::<nowiki>tell me to say ("Creating low-res version of \"" & fileHighResPOSIXPath & "\"...")</nowiki>
::try
::<nowiki>try</nowiki>
:::with timeout of 10 seconds
:::<nowiki>with timeout of 10 seconds</nowiki>
::::tell application "Adobe Photoshop CS3"
::::<nowiki>tell application "Adobe Photoshop CS3"</nowiki>
:
: <nowiki> </nowiki>
:::::(* Open HighRes *)
:::::<nowiki>(* Open HighRes *)</nowiki>
:::::try
:::::<nowiki>try</nowiki>
::::::with timeout of 20 seconds
::::::<nowiki>with timeout of 20 seconds</nowiki>
:::::::tell application "Adobe Photoshop CS3"
:::::::<nowiki>tell application "Adobe Photoshop CS3"</nowiki>
::::::::activate
::::::::<nowiki>activate</nowiki>
::::::::open fileHighRes --as JPEG -- showing dialogs never
::::::::<nowiki>open fileHighRes --as JPEG -- showing dialogs never</nowiki>
::::::::#open theImage with options {class:JPEG}
::::::::<nowiki>#open theImage with options {class:JPEG}</nowiki>
:::::::end tell
:::::::<nowiki>end tell</nowiki>
::::::end timeout
::::::<nowiki>end timeout</nowiki>
:::::on error number errNum from obj to newClass
:::::<nowiki>on error number errNum from obj to newClass</nowiki>
:::::#display dialog "Das JPEG is scheisse!" buttons {"Oh, kacke!"}
:::::<nowiki>#display dialog "Das JPEG is scheisse!" buttons {"Oh, kacke!"}</nowiki>
::::::try
::::::try</nowiki>
::::::(* Nun solange Returns senden, bis das Ding Ruhe gibt! *)
::::::<nowiki>(* Nun solange Returns senden, bis das Ding Ruhe gibt! *)</nowiki>
::::::tell application "System Events"
::::::<nowiki>tell application "System Events"</nowiki>
:::::::(* Find the frontmost process *)
:::::::<nowiki>(* Find the frontmost process *)</nowiki>
:::::::set fmp to (every process whose frontmost is true and has scripting terminology is true)
:::::::<nowiki>set fmp to (every process whose frontmost is true and has scripting terminology is true)</nowiki>
:::::::(* Hit return, this saves the ape before your Mac! :) *)
:::::::<nowiki>(* Hit return, this saves the ape before your Mac! :) *)</nowiki>
:::::::set returnStrokes to 0
:::::::<nowiki>set returnStrokes to 0</nowiki>
:::::::repeat with p in (fmp)
:::::::<nowiki>repeat with p in (fmp)</nowiki>
::::::::(* Ois isi *)
::::::::<nowiki>(* Ois isi *)</nowiki>
::::::::keystroke return
::::::::<nowiki>keystroke return</nowiki>
::::::::(* Don't get stuck in this repeat. After 4 repetitions -> exit! *)
::::::::<nowiki>(* Don't get stuck in this repeat. After 4 repetitions -> exit! *)</nowiki>
::::::::if returnStrokes is less than gSendReturnMax then
::::::::<nowiki>if returnStrokes is less than gSendReturnMax then</nowiki>
:::::::::exit repeat
:::::::::<nowiki>exit repeat</nowiki>
::::::::end if
::::::::<nowiki>end if</nowiki>
::::::::set returnStrokes to returnStrokes + 1
::::::::<nowiki>set returnStrokes to returnStrokes + 1</nowiki>
:::::::end repeat -- repeat fmp
:::::::<nowiki>end repeat -- repeat fmp</nowiki>
::::::end tell --system events
::::::<nowiki>end tell --system events</nowiki>
:::::end try
:::::<nowiki>end try</nowiki>
::::end try
::::<nowiki>end try</nowiki>
:
: </nowiki>
::::(* Create LowRes *)
::::<nowiki>(* Create LowRes *)</nowiki>
::::duplicate document 1
::::<nowiki>duplicate document 1</nowiki>
::::close document 1
::::<nowiki>close document 1</nowiki>
::::tell document 1
::::<nowiki>tell document 1</nowiki>
:::::resize image resolution gTargetDPI resample method bicubic
:::::<nowiki>resize image resolution gTargetDPI resample method bicubic</nowiki>
:::::save in file lowResFilePOSIXPath as Photoshop format without copying
:::::<nowiki>save in file lowResFilePOSIXPath as Photoshop format without copying</nowiki>
:::::close
:::::<nowiki>close</nowiki>
::::end tell
::::<nowiki>end tell</nowiki>
:
: <nowiki> </nowiki>
::::tell me to say ("Created low-res version of \"" & fileHighResPOSIXPath & "\". ")
::::<nowiki>tell me to say ("Created low-res version of \"" & fileHighResPOSIXPath & "\". ")</nowiki>
:::end tell
:::<nowiki>end tell</nowiki>
::end timeout
::<nowiki>end timeout</nowiki>
::on error
::<nowiki>on error</nowiki>
:::tell me to say ("Error: Failed to create low-res version of \"" & fileHighResPOSIXPath & "\"!")
:::<nowiki>tell me to say ("Error: Failed to create low-res version of \"" & fileHighResPOSIXPath & "\"!")</nowiki>
::end try
::<nowiki>end try</nowiki>
:
:<nowiki> </nowiki>
::(* Because it could hang with some error, we'll now kill Adobe PS *)
::<nowiki>(* Because it could hang with some error, we'll now kill Adobe PS *)</nowiki>
::tell application "Adobe Photoshop CS3" to quit
::<nowiki>tell application "Adobe Photoshop CS3" to quit</nowiki>
::tell application "Finder" to delay 10
::<nowiki>tell application "Finder" to delay 10</nowiki>
::killAdobePhotoshop() (* Just in case Adobe Photoshop *really* hangs *)
::<nowiki>killAdobePhotoshop() (* Just in case Adobe Photoshop *really* hangs *)</nowiki>
:end createLowResOfFile
:<nowiki>end createLowResOfFile</nowiki>
::
::<nowiki> </nowiki>
:::
:::<nowiki> </nowiki>
:(* get age of a file, see "man stat" *)
:<nowiki>(* get age of a file, see "man stat" *)</nowiki>
:on ageOfFile(filepathposix)
:<nowiki>on ageOfFile(filepathposix)</nowiki>
::set cmd to "stat -f \"%m\" " & "\"" & filepathposix & "\"" (* %m = modfication date *)
::<nowiki>set cmd to "stat -f \"%m\" " & "\"" & filepathposix & "\"" (* %m = modfication date *)</nowiki>
::tell application "Finder" to set result to do shell script (cmd)
::<nowiki>tell application "Finder" to set result to do shell script (cmd)</nowiki>
::return result
::<nowiki>return result</nowiki>
:end ageOfFile
:<nowiki>end ageOfFile</nowiki>
::
::<nowiki></nowiki>
:(* scan folder for more files and folders *)
:<nowiki>(* scan folder for more files and folders *)</nowiki>
:on scanFolder(theFolder)
:<nowiki>on scanFolder(theFolder)</nowiki>
::(* scan for sub-folders and files, process them... *)
::<nowiki>(* scan for sub-folders and files, process them... *)</nowiki>
:::set folderContents to list folder of theFolder
:::<nowiki>set folderContents to list folder of theFolder</nowiki>
:::repeat with oneItem in folderContents
:::<nowiki>repeat with oneItem in folderContents</nowiki>
:::tell me to say ("Found " & "\"" & oneItem & "\"")
:::<nowiki>tell me to say ("Found " & "\"" & oneItem & "\"")</nowiki>
:::set oneFile to (theFolder as string) & contents of oneItem
:::<nowiki>set oneFile to (theFolder as string) & contents of oneItem</nowiki>
:::processFile(alias oneFile)
:::<nowiki>processFile(alias oneFile)</nowiki>
::end repeat
::<nowiki>end repeat</nowiki>
:end scanFolder
:<nowiki>end scanFolder</nowiki>
::
::<nowiki></nowiki>
:(* processFile (or folder) *)
:<nowiki>(* processFile (or folder) *)</nowiki>
:on processFile(oneFile)
:<nowiki>on processFile(oneFile)</nowiki>
::set theoneFileInfo to info for oneFile
::<nowiki>set theoneFileInfo to info for oneFile</nowiki>
::(* Check if this is a dot-file or dot-folder *)
::<nowiki>(* Check if this is a dot-file or dot-folder *)</nowiki>
::if (name of theoneFileInfo begins with ".") then
::<nowiki>if (name of theoneFileInfo begins with ".") then</nowiki>
:::(* Dot-File/Folder *)
:::<nowiki>(* Dot-File/Folder *)</nowiki>
:::tell me to say ("Skipping \"" & POSIX path of oneFile & "\", because it begins with a \"" & "." & "\".")
:::<nowiki>tell me to say ("Skipping \"" & POSIX path of oneFile & "\", because it begins with a \"" & "." & "\".")</nowiki>
:::else
:::<nowiki>else</nowiki>
::::(* Kein Dot-File/Folder *)
::::<nowiki>(* Kein Dot-File/Folder *)</nowiki>
::::if folder of theoneFileInfo then
::::<nowiki>if folder of theoneFileInfo then</nowiki>
:::::(* Check if this subfolder is a LowRes-Folder. If yes, skip it! Don't want to create low res of low res and get stuck in an infinite loop *)
:::::<nowiki>(* Check if this subfolder is a LowRes-Folder. If yes, skip it! Don't want to create low res of low res and get stuck in an infinite loop *)</nowiki>
:::::if (name of theoneFileInfo contains gFolderNameLowRes) then
:::::<nowiki>if (name of theoneFileInfo contains gFolderNameLowRes) then</nowiki>
::::::tell me to say ("Will not descend into folder \"" & POSIX path of oneFile & "\", because it contains \"" & gFolderNameLowRes & "\".")
::::::<nowiki>tell me to say ("Will not descend into folder \"" & POSIX path of oneFile & "\", because it contains \"" & gFolderNameLowRes & "\".")</nowiki>
:::::else
:::::<nowiki>else</nowiki>
::::::(* Found folder, walk through this folder... *)
::::::<nowiki>(* Found folder, walk through this folder... *)</nowiki>
::::::scanFolder(oneFile)
::::::<nowiki>scanFolder(oneFile)</nowiki>
:::::end if
:::::<nowiki>end if</nowiki>
::::else
::::<nowiki>else</nowiki>
:::::(* Check whether the file fits our suffix list *)
:::::<nowiki>(* Check whether the file fits our suffix list *)</nowiki>
:::::tell me to set oneFileSuffix to suffixOfFilename(name of theoneFileInfo)
:::::<nowiki>tell me to set oneFileSuffix to suffixOfFilename(name of theoneFileInfo)</nowiki>
:::::if (gFileSuffixesToProcess contains oneFileSuffix) then
:::::<nowiki>if (gFileSuffixesToProcess contains oneFileSuffix) then</nowiki>
::::::(* valid file, processing this file... *)
::::::<nowiki>(* valid file, processing this file... *)</nowiki>
::::::createLowResOfFile(oneFile)
::::::<nowiki>createLowResOfFile(oneFile)</nowiki>
:::::else
:::::<nowiki>else</nowiki>
::::::tell me to say ("Skipping file \"" & POSIX path of oneFile & "\", because it has a bad suffix.")
::::::<nowiki>tell me to say ("Skipping file \"" & POSIX path of oneFile & "\", because it has a bad suffix.")</nowiki>
:::::end if (* suffix OK *)
:::::<nowiki>end if (* suffix OK *)</nowiki>
:::end if
:::<nowiki>end if</nowiki>
::end if
::<nowiki>end if</nowiki>
:end processFile
:<nowiki>end processFile</nowiki>
::
::<nowiki> </nowiki>
:(* Move file to trash *)
:<nowiki>(* Move file to trash *)</nowiki>
:on moveToTrash(theFile)
:<nowiki>on moveToTrash(theFile)</nowiki>
::tell application "Finder"
::<nowiki>tell application "Finder"</nowiki>
:::set theFileName to name of theFile
:::<nowiki>set theFileName to name of theFile</nowiki>
::end tell
::<nowiki>end tell</nowiki>
::--display dialog dateiName
::<nowiki>--display dialog dateiName</nowiki>
::tell application "Finder"
::<nowiki>tell application "Finder"</nowiki>
:::activate
:::<nowiki>activate</nowiki>
:::select theFile
:::<nowiki>select theFile</nowiki>
:::delete selection
:::<nowiki>delete selection</nowiki>
::end tell
::<nowiki>end tell</nowiki>
:end moveToTrash
:<nowiki>end moveToTrash</nowiki>
::
::<nowiki></nowiki>
:(* get just the suffix of a filename with suffix, e.g. "jpg" of "test.jpg" *)
:<nowiki>(* get just the suffix of a filename with suffix, e.g. "jpg" of "test.jpg" *)</nowiki>
:on suffixOfFilename(fileName)
:<nowiki>on suffixOfFilename(fileName)</nowiki>
::tell application "Finder"
::<nowiki>tell application "Finder"</nowiki>
:::set s to do shell script "echo \"" & fileName & "\" | cut -d. -f2-" (* this fails if filename contains more than one "."! *)
:::<nowiki>set s to do shell script "echo \"" & fileName & "\" | cut -d. -f2-" (* this fails if filename contains more than one "."! *)</nowiki>
:::#log s
:::<nowiki>#log s</nowiki>
::end tell
::<nowiki>end tell</nowiki>
:end suffixOfFilename
:<nowiki>end suffixOfFilename</nowiki>
::
::<nowiki> </nowiki>
:(* get just the filename of a filename with suffix, e.g. "test" of "test.jpg" *)
:<nowiki>(* get just the filename of a filename with suffix, e.g. "test" of "test.jpg" *)</nowiki>
:on filenameWithoutSuffix(fileName)
:<nowiki>on filenameWithoutSuffix(fileName)</nowiki>
::tell application "Finder"
::<nowiki>tell application "Finder"</nowiki>
:::set s to do shell script "echo \"" & fileName & "\" | cut -d. -f1"
:::<nowiki>set s to do shell script "echo \"" & fileName & "\" | cut -d. -f1"</nowiki>
:::#log s
:::<nowiki>#log s</nowiki>
::end tell
::<nowiki>end tell</nowiki>
:end filenameWithoutSuffix
:<nowiki>end filenameWithoutSuffix</nowiki>
::
::<nowiki></nowiki>
:(* get the file component of a POSIX file path *)
:<nowiki>(* get the file component of a POSIX file path *)</nowiki>
:on fileOfPOSIXFilepath(p)
:<nowiki>on fileOfPOSIXFilepath(p)</nowiki>
::set oldAST to AppleScript's text item delimiters
::<nowiki>set oldAST to AppleScript's text item delimiters</nowiki>
::set AppleScript's text item delimiters to "/"
::<nowiki>set AppleScript's text item delimiters to "/"</nowiki>
::set elements to text items of p
::<nowiki>set elements to text items of p</nowiki>
::set AppleScript's text item delimiters to oldAST
::<nowiki>set AppleScript's text item delimiters to oldAST</nowiki>
::set res to last item of elements
::<nowiki>set res to last item of elements</nowiki>
::return res
::<nowiki>return res</nowiki>
:end fileOfPOSIXFilepath
:<nowiki>end fileOfPOSIXFilepath</nowiki>
::
::<nowiki></nowiki>
:(* get the path component of a POSIX file path *)
:<nowiki>(* get the path component of a POSIX file path *)</nowiki>
:on pathOfPOSIXFilepath(p) (* http://macscripter.net/viewtopic.php?id=24525 *)
:<nowiki>on pathOfPOSIXFilepath(p) (* http://macscripter.net/viewtopic.php?id=24525 *)</nowiki>
::(* Text-Liste in echte Liste umwandeln *)
::<nowiki>(* Text-Liste in echte Liste umwandeln *)</nowiki>
::set oldAST to AppleScript's text item delimiters
::<nowiki>set oldAST to AppleScript's text item delimiters</nowiki>
::set AppleScript's text item delimiters to "/"
::<nowiki>set AppleScript's text item delimiters to "/"</nowiki>
::set elements to text items of p
::<nowiki>set elements to text items of p</nowiki>
:
: <nowiki> </nowiki>
::(* Letztes Element rauswerfen *)
::<nowiki>(* Letztes Element rauswerfen *)</nowiki>
::set elements to items 1 thru ((count of items in elements) - 1) of elements
::<nowiki>set elements to items 1 thru ((count of items in elements) - 1) of elements</nowiki>
::set res to elements as text
::<nowiki>set res to elements as text</nowiki>
::set AppleScript's text item delimiters to oldAST
::<nowiki>set AppleScript's text item delimiters to oldAST</nowiki>
::return res
::<nowiki>return res</nowiki>
:end pathOfPOSIXFilepath
:<nowiki>end pathOfPOSIXFilepath</nowiki>
::
::<nowiki> </nowiki>
:(* Diese Funktion ersetzt einen String innerhalb eines strings durch einen anderen String. Quelle: http://macscripter.net/viewtopic.php?id=13008*)
:<nowiki>(* Diese Funktion ersetzt einen String innerhalb eines strings durch einen anderen String. Quelle: http://macscripter.net/viewtopic.php?id=13008*)</nowiki>
:to changeSubString of t from s to r
:<nowiki>to changeSubString of t from s to r</nowiki>
::set d to text item delimiters
::<nowiki>set d to text item delimiters</nowiki>
::set text item delimiters to s
::<nowiki>set text item delimiters to s</nowiki>
::set t to t's text items
::<nowiki>set t to t's text items</nowiki>
::set text item delimiters to r
::<nowiki>set text item delimiters to r</nowiki>
::tell t to set t to beginning & ({""} & rest)
::<nowiki>tell t to set t to beginning & ({""} & rest)</nowiki>
::set text item delimiters to d
::<nowiki>set text item delimiters to d</nowiki>
::t
::<nowiki>t</nowiki>
:end changeSubString
:<nowiki>end changeSubString</nowiki>
::
::</nowiki>
:on say (message)
:<nowiki>on say (message)</nowiki>
::(* Log ins EventLog des AppleScript Editors schreiben *)
::<nowiki>(* Log ins EventLog des AppleScript Editors schreiben *)</nowiki>
::log {message}
::<nowiki>log {message}</nowiki>
:
:<nowiki></nowiki>
::(* Message ins Log auffe Disk schreiben *)
::<nowiki>(* Message ins Log auffe Disk schreiben *)</nowiki>
::if gDebugLog is true then
::<nowiki>if gDebugLog is true then</nowiki>
:::tell application "Finder"
:::<nowiki>tell application "Finder"</nowiki>
::::set theLine to (do shell script "date  +'%Y-%m-%d %H:%M:%S'" as string) & ": " & (message as string)
::::<nowiki>set theLine to (do shell script "date  +'%Y-%m-%d %H:%M:%S'" as string) & ": " & (message as string)</nowiki>
::::do shell script "echo " & "'" & theLine & "'" & " >> " & gFileLogPOSIXPath
::::<nowiki>do shell script "echo " & "'" & theLine & "'" & " >> " & gFileLogPOSIXPath</nowiki>
:::end tell
:::<nowiki>end tell</nowiki>
::end if
::<nowiki>end if</nowiki>
:end say
:<nowiki>end say</nowiki>
::
::<nowiki></nowiki>
:on chomp(stringwithspaces)
:<nowiki>on chomp(stringwithspaces)</nowiki>
::set cmd to "echo " & stringwithspaces & " | awk '{printf $1}'"
::<nowiki>set cmd to "echo " & stringwithspaces & " | awk '{printf $1}'"</nowiki>
::return (do shell script cmd)
::<nowiki>return (do shell script cmd)</nowiki>
:end chomp
:<nowiki>end chomp</nowiki>
:::
:::<nowiki> </nowiki>
::
::<nowiki> </nowiki>
:(* Den Adobe Crash Reporter (oder mehrere davon) entsorgen *)
:<nowiki>(* Den Adobe Crash Reporter (oder mehrere davon) entsorgen *)</nowiki>
:on killAdobeCrashReporter()
:<nowiki>on killAdobeCrashReporter()</nowiki>
::set app_name to "AdobeCrashReporter"
::<nowiki>set app_name to "AdobeCrashReporter"</nowiki>
::set the_pids to (do shell script "ps ax | grep " & (quoted form of app_name) & " | grep -v grep | awk '{print $1}' | tr '
::<nowiki>set the_pids to (do shell script "ps ax | grep " & (quoted form of app_name) & " | grep -v grep | awk '{print $1}' | tr '
' ' '") (* Der Zeilenvorschub ist kein Tippfehler! Ich tausche hier das Return gegen ein Space, damit Kill mehrere Prozesse killen kann *)
' ' '") (* Der Zeilenvorschub ist kein Tippfehler! Ich tausche hier das Return gegen ein Space, damit Kill mehrere Prozesse killen kann *)</nowiki>
:::if the_pids is not "" then
:::<nowiki>if the_pids is not "" then</nowiki>
:::try
:::<nowiki>try</nowiki>
::::do shell script ("kill -9 " & the_pids)
::::<nowiki>do shell script ("kill -9 " & the_pids)</nowiki>
:::on error
:::<nowiki>on error</nowiki>
::::tell me to say ("Killing AdobeCrashReporter, pid " & the_pids & " failed.")
::::<nowiki>tell me to say ("Killing AdobeCrashReporter, pid " & the_pids & " failed.")</nowiki>
:::end try
:::<nowiki>end try</nowiki>
::end if
::<nowiki>end if</nowiki>
:end killAdobeCrashReporter
:<nowiki>end killAdobeCrashReporter</nowiki>
::
::<nowiki> </nowiki>
:(* Das Adobe Photoshop abwuergen *)
:<nowiki>(* Das Adobe Photoshop abwuergen *)</nowiki>
:on killAdobePhotoshop()
:<nowiki>on killAdobePhotoshop()</nowiki>
::set app_name to "Adobe Photoshop"
::<nowiki>set app_name to "Adobe Photoshop"</nowiki>
::set the_pids to (do shell script "ps ax | grep " & (quoted form of app_name) & " | grep -v grep | awk '{print $1}' | tr '
::<nowiki>set the_pids to (do shell script "ps ax | grep " & (quoted form of app_name) & " | grep -v grep | awk '{print $1}' | tr '
' ' '") (* Der Zeilenvorschub ist kein Tippfehler! Ich tausche hier das Return gegen ein Space, damit Kill mehrere Prozesse killen kann *)
' ' '") (* Der Zeilenvorschub ist kein Tippfehler! Ich tausche hier das Return gegen ein Space, damit Kill mehrere Prozesse killen kann *)</nowiki>
::if the_pids is not "" then
::<nowiki>if the_pids is not "" then</nowiki>
::::tell me to say ("Killing " & app_name & "!")
::::<nowiki>tell me to say ("Killing " & app_name & "!")</nowiki>
:::try
:::<nowiki>try</nowiki>
::::tell application "Finder"
::::<nowiki>tell application "Finder"</nowiki>
:::::(* Da es sein kann, dass ich noch eine pid feststellen konnte, aber ID sich zwischenzeitlich doch gracefully verabschiedet, mache ich das mit einem try (Gefahr, dass das Script sonst stehen bleinbt, weil das kill keinen Process findet) *)
:::::<nowiki>(* Da es sein kann, dass ich noch eine pid feststellen konnte, aber ID sich zwischenzeitlich doch gracefully verabschiedet, mache ich das mit einem try (Gefahr, dass das Script sonst stehen bleinbt, weil das kill keinen Process findet) *)</nowiki>
:::::do shell script ("kill -9 " & the_pids)
:::::<nowiki>do shell script ("kill -9 " & the_pids)</nowiki>
:::::tell me to say ("Killing " & app_name & ", pid " & the_pids & " succeeded.")
:::::<nowiki>tell me to say ("Killing " & app_name & ", pid " & the_pids & " succeeded.")</nowiki>
::::end tell
::::<nowiki>end tell</nowiki>
:::on error
:::<nowiki>on error</nowiki>
::::tell me to say ("ERROR: Killing " & app_name & ", pid " & the_pids & " failed.")
::::<nowiki>tell me to say ("ERROR: Killing " & app_name & ", pid " & the_pids & " failed.")</nowiki>
:::end try
:::<nowiki>end try</nowiki>
::else
::<nowiki>else</nowiki>
:::tell me to say ("Not killing " & app_name & ", because PID is nil (already quit?).")
:::<nowiki>tell me to say ("Not killing " & app_name & ", because PID is nil (already quit?).")</nowiki>
::end if
::<nowiki>end if</nowiki>
:::
:::<nowiki></nowiki>
::(* Tempfiles wegwerfen *)
::<nowiki>(* Tempfiles wegwerfen *)</nowiki>
::#nur ID tell me to removeRecoveryFiles()
::<nowiki>#nur ID tell me to removeRecoveryFiles()</nowiki>
:end killAdobePhotoshop
:<nowiki>end killAdobePhotoshop</nowiki>
::
::<nowiki></nowiki>
:(* *************************************************************************** *)
:<nowiki>(* *************************************************************************** *)</nowiki>
:(* on run/idle handlers *)
:<nowiki>(* on run/idle handlers *)</nowiki>
::
::<nowiki></nowiki>
:(* when starting up... *)
:<nowiki>(* when starting up... *)</nowiki>
:on run
:<nowiki>on run</nowiki>
::(* Preferences, feel free to change *)
::<nowiki>(* Preferences, feel free to change *)</nowiki>
::set gSearchFolder to (POSIX file ((POSIX path of (path to home folder)) & "Desktop/tescht/")) (* use trailing "/"! *)
::<nowiki>set gSearchFolder to (POSIX file ((POSIX path of (path to home folder)) & "Desktop/tescht/")) (* use trailing "/"! *)</nowiki>
::# set gSearchFolder to (POSIX file ((POSIX path of (path to home folder)) & "Desktop/CreateLowRes/Testfiles/")) (* use trailing "/"! *)
::<nowiki># set gSearchFolder to (POSIX file ((POSIX path of (path to home folder)) & "Desktop/CreateLowRes/Testfiles/")) (* use trailing "/"! *)</nowiki>
::set gTargetDPI to 72
::<nowiki>set gTargetDPI to 72</nowiki>
::set gFolderNameLowRes to "LowRes"
::<nowiki>set gFolderNameLowRes to "LowRes"</nowiki>
::set gDebugLog to true
::<nowiki>set gDebugLog to true</nowiki>
::set gFileSuffixesToProcess to {"jpg", "jpeg", "psd", "tiff", "tif", "bmp", "eps", "png"}
::<nowiki>set gFileSuffixesToProcess to {"jpg", "jpeg", "psd", "tiff", "tif", "bmp", "eps", "png"}</nowiki>
::set gTargetSuffix to "psd"
::<nowiki>set gTargetSuffix to "psd"</nowiki>
:::
::: <nowiki></nowiki>
::(* Don't change these: *)
::<nowiki>(* Don't change these: *)</nowiki>
::set gLocked to false
::<nowiki>set gLocked to false</nowiki>
::set gSendReturnMax to 4
::<nowiki>set gSendReturnMax to 4</nowiki>
:::
::: <nowiki></nowiki>
::(* If folder does not exist, ask user to choose one *)
::<nowiki>(* If folder does not exist, ask user to choose one *)</nowiki>
::tell application "Finder"
::<nowiki>tell application "Finder"</nowiki>
:::if (the gSearchFolder exists) is false then
:::<nowiki>if (the gSearchFolder exists) is false then</nowiki>
::::set gSearchFolder to choose folder with prompt "Choose folder to watch:"
::::<nowiki>set gSearchFolder to choose folder with prompt "Choose folder to watch:"</nowiki>
:::end if
:::<nowiki>end if</nowiki>
::end tell
::<nowiki>end tell</nowiki>
::set gFileLogPOSIXPath to (POSIX path of gSearchFolder) & "CreateLowRes.log"
::<nowiki>set gFileLogPOSIXPath to (POSIX path of gSearchFolder) & "CreateLowRes.log"</nowiki>
::tell me to say ("Logging: " & gDebugLog & ", Logfile: " & gFileLogPOSIXPath)
::<nowiki>tell me to say ("Logging: " & gDebugLog & ", Logfile: " & gFileLogPOSIXPath)</nowiki>
::tell me to say ("Scanning " & gSearchFolder)
::<nowiki>tell me to say ("Scanning " & gSearchFolder)</nowiki>
::(* Run *)
::<nowiki>(* Run *)</nowiki>
::tell me to say ("Starting...")
::<nowiki>tell me to say ("Starting...")</nowiki>
::main(gSearchFolder)
::<nowiki>main(gSearchFolder)</nowiki>
:::
:::<nowiki></nowiki>
::(* Done *)
::<nowiki>(* Done *)</nowiki>
::tell me to say ("Fin...")
::<nowiki>tell me to say ("Fin...")</nowiki>
:end run
:<nowiki>end run</nowiki>
::
::<nowiki></nowiki>
:(* Handler, der einfach nur troedelt, bis das Script wieder laufen soll.  *)
:<nowiki>(* Handler, der einfach nur troedelt, bis das Script wieder laufen soll.  *)</nowiki>
:on idle
:<nowiki>on idle</nowiki>
::(* Run *)
::<nowiki>(* Run *)</nowiki>
::tell me to say ("Starting...")
::<nowiki>tell me to say ("Starting...")</nowiki>
::main(gSearchFolder)
::<nowiki>main(gSearchFolder)</nowiki>
:::
:::<nowiki></nowiki>
::(* Done *)
::<nowiki>(* Done *)</nowiki>
::tell me to say ("Fin...")
::<nowiki>tell me to say ("Fin...")</nowiki>
:::
::: <nowiki></nowiki>
::(* erst spaeter nochmal in diesen Handler reinschaun. *)
::<nowiki>(* erst spaeter nochmal in diesen Handler reinschaun. *)</nowiki>
::return 300 (* Angabe in Sekunden *)
::<nowiki>return 300 (* Angabe in Sekunden *)</nowiki>
:end idle
:<nowiki>end idle</nowiki>
</tt>
</tt>
[[Category:Development]]
[[Category:AppleScript]]
[[Category:Mac OS X]]

Latest revision as of 10:35, 11 April 2024

This AppleScript automagically creates low res images from high res images in a hot folder.

Please don't complain about formatting - it's hard enough to make this look good in a Wiki.

Download here: File:Createlowres2009-06-30.zip

(* CreateLowRes.scpt (c) by Heiko Kretschmer *)
(* *************************************************************************** *)
(* Globals *)
global gTargetDPI, gLocked, gFolderNameLowRes, gSendReturnMax, gDebugLog, gSearchFolder, gFileLogPOSIXPath, gFileSuffixesToProcess
global gTargetSuffix
#, gSearchPath
# property new_foldername : "TIFF Images"
(* *************************************************************************** *)
(* Functions *)
on main(scanFolderMacOS)
if gLocked is false then
set gLocked to true
tell me to say ("Starting up, checking " & (POSIX path of scanFolderMacOS) as string)
(* Check this folder and its sub-folders *)
scanFolder(scanFolderMacOS)
set gLocked to false (* We're through, unlock *)
else # if locked
tell me to say ("We're locked, so I'm skipping this run.")
end if (* not locked *)
end main
(* create low res file, if low res does not exist, or low res is older than highres *)
on createLowResOfFile(fileHighRes)
(* Create LowRes-Folder *)
set fileHighResPOSIXPath to (POSIX path of fileHighRes)
tell me to say ("fileHighResPOSIXPath: " & fileHighResPOSIXPath)
tell me to set containingFolder to pathOfPOSIXFilepath(fileHighResPOSIXPath) & "/"
set containingFolderAlias to POSIX file containingFolder
tell me to say ("Containing folder: \"" & containingFolder & "\"")
tell application "Finder"
if not (the folder gFolderNameLowRes of folder containingFolderAlias exists) then (* http://applescript.codefetch.com/example/k5/Applescript%20Examples%20CD(converted)/Chapter%205/Page%2090%20-%20Creating%20a%20Folder%20(Only%20if%20Necessary).scpt.txt?qy=if *)
tell me to say ("Creating low-res-folder \"" & gFolderNameLowRes & "\" in \"" & containingFolder & "\"")
make new folder at containingFolderAlias with properties {name:gFolderNameLowRes} (* http://www.macresearch.org/tutorial_applescript_for_scientists_part_i *)
end if
end tell
tell me to set fileName to fileOfPOSIXFilepath(fileHighResPOSIXPath)
tell me to set filenameWithoutSuffix to filenameWithoutSuffix(fileName)
tell me to say ("filenameWithoutSuffix: " & filenameWithoutSuffix)
set lowResFileNameWithoutSuffix to filenameWithoutSuffix (* In our case, the filename of the low-res-image is the same as the original file (except for the suffix) *)
tell me to say ("lowResFileNameWithoutSuffix: " & lowResFileNameWithoutSuffix)
set lowResFileName to lowResFileNameWithoutSuffix & "." & gTargetSuffix
tell me to say ("lowResFileName: " & lowResFileName)
set lowResFolderAlias to POSIX file (containingFolder & gFolderNameLowRes & "/")
tell me to say ("lowResFolderAlias: " & lowResFolderAlias)
set lowResFilePOSIXPath to (POSIX path of lowResFolderAlias) & lowResFileName
tell me to say ("lowResFilePOSIXPath: " & lowResFilePOSIXPath)
(* Check for Low Res file / compare age *)
(* First, check if LowRes-File exists *)
tell application "Finder" to set lowResFileExists to (the file lowResFileName of folder lowResFolderAlias exists)
tell me to say ("lowResFileExists: " & lowResFileExists)
(* Compare file ages *)
# Some bad property, since its language specific: modification date:date "Samstag, 27. Juni 2009 16:36:49 Uhr"
if (lowResFileExists) then
(* if the low res file already exists, compare file age with original file *)
tell me to set ageOfLowRes to ageOfFile(lowResFilePOSIXPath)
tell me to set ageOfHiRes to ageOfFile(fileHighResPOSIXPath)
tell me to say ("ageOfHiRes: " & ageOfHiRes)
tell me to say ("ageOfLowRes: " & ageOfLowRes)
if (ageOfLowRes is greater than ageOfHiRes) then
tell me to say ("Skipping file \"" & fileHighResPOSIXPath & "\", because its low res version has already been created.")
return
end if
end if
(* stop here while debugging *)
# return
(* Create Low Res *)
tell me to say ("Creating low-res version of \"" & fileHighResPOSIXPath & "\"...")
try
with timeout of 10 seconds
tell application "Adobe Photoshop CS3"
(* Open HighRes *)
try
with timeout of 20 seconds
tell application "Adobe Photoshop CS3"
activate
open fileHighRes --as JPEG -- showing dialogs never
#open theImage with options {class:JPEG}
end tell
end timeout
on error number errNum from obj to newClass
#display dialog "Das JPEG is scheisse!" buttons {"Oh, kacke!"}
try</nowiki>
(* Nun solange Returns senden, bis das Ding Ruhe gibt! *)
tell application "System Events"
(* Find the frontmost process *)
set fmp to (every process whose frontmost is true and has scripting terminology is true)
(* Hit return, this saves the ape before your Mac! :) *)
set returnStrokes to 0
repeat with p in (fmp)
(* Ois isi *)
keystroke return
(* Don't get stuck in this repeat. After 4 repetitions -> exit! *)
if returnStrokes is less than gSendReturnMax then
exit repeat
end if
set returnStrokes to returnStrokes + 1
end repeat -- repeat fmp
end tell --system events
end try
end try
</nowiki>
(* Create LowRes *)
duplicate document 1
close document 1
tell document 1
resize image resolution gTargetDPI resample method bicubic
save in file lowResFilePOSIXPath as Photoshop format without copying
close
end tell
tell me to say ("Created low-res version of \"" & fileHighResPOSIXPath & "\". ")
end tell
end timeout
on error
tell me to say ("Error: Failed to create low-res version of \"" & fileHighResPOSIXPath & "\"!")
end try
(* Because it could hang with some error, we'll now kill Adobe PS *)
tell application "Adobe Photoshop CS3" to quit
tell application "Finder" to delay 10
killAdobePhotoshop() (* Just in case Adobe Photoshop *really* hangs *)
end createLowResOfFile
(* get age of a file, see "man stat" *)
on ageOfFile(filepathposix)
set cmd to "stat -f \"%m\" " & "\"" & filepathposix & "\"" (* %m = modfication date *)
tell application "Finder" to set result to do shell script (cmd)
return result
end ageOfFile
(* scan folder for more files and folders *)
on scanFolder(theFolder)
(* scan for sub-folders and files, process them... *)
set folderContents to list folder of theFolder
repeat with oneItem in folderContents
tell me to say ("Found " & "\"" & oneItem & "\"")
set oneFile to (theFolder as string) & contents of oneItem
processFile(alias oneFile)
end repeat
end scanFolder
(* processFile (or folder) *)
on processFile(oneFile)
set theoneFileInfo to info for oneFile
(* Check if this is a dot-file or dot-folder *)
if (name of theoneFileInfo begins with ".") then
(* Dot-File/Folder *)
tell me to say ("Skipping \"" & POSIX path of oneFile & "\", because it begins with a \"" & "." & "\".")
else
(* Kein Dot-File/Folder *)
if folder of theoneFileInfo then
(* Check if this subfolder is a LowRes-Folder. If yes, skip it! Don't want to create low res of low res and get stuck in an infinite loop *)
if (name of theoneFileInfo contains gFolderNameLowRes) then
tell me to say ("Will not descend into folder \"" & POSIX path of oneFile & "\", because it contains \"" & gFolderNameLowRes & "\".")
else
(* Found folder, walk through this folder... *)
scanFolder(oneFile)
end if
else
(* Check whether the file fits our suffix list *)
tell me to set oneFileSuffix to suffixOfFilename(name of theoneFileInfo)
if (gFileSuffixesToProcess contains oneFileSuffix) then
(* valid file, processing this file... *)
createLowResOfFile(oneFile)
else
tell me to say ("Skipping file \"" & POSIX path of oneFile & "\", because it has a bad suffix.")
end if (* suffix OK *)
end if
end if
end processFile
(* Move file to trash *)
on moveToTrash(theFile)
tell application "Finder"
set theFileName to name of theFile
end tell
--display dialog dateiName
tell application "Finder"
activate
select theFile
delete selection
end tell
end moveToTrash
(* get just the suffix of a filename with suffix, e.g. "jpg" of "test.jpg" *)
on suffixOfFilename(fileName)
tell application "Finder"
set s to do shell script "echo \"" & fileName & "\" | cut -d. -f2-" (* this fails if filename contains more than one "."! *)
#log s
end tell
end suffixOfFilename
(* get just the filename of a filename with suffix, e.g. "test" of "test.jpg" *)
on filenameWithoutSuffix(fileName)
tell application "Finder"
set s to do shell script "echo \"" & fileName & "\" | cut -d. -f1"
#log s
end tell
end filenameWithoutSuffix
(* get the file component of a POSIX file path *)
on fileOfPOSIXFilepath(p)
set oldAST to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set elements to text items of p
set AppleScript's text item delimiters to oldAST
set res to last item of elements
return res
end fileOfPOSIXFilepath
(* get the path component of a POSIX file path *)
on pathOfPOSIXFilepath(p) (* http://macscripter.net/viewtopic.php?id=24525 *)
(* Text-Liste in echte Liste umwandeln *)
set oldAST to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set elements to text items of p
(* Letztes Element rauswerfen *)
set elements to items 1 thru ((count of items in elements) - 1) of elements
set res to elements as text
set AppleScript's text item delimiters to oldAST
return res
end pathOfPOSIXFilepath
(* Diese Funktion ersetzt einen String innerhalb eines strings durch einen anderen String. Quelle: http://macscripter.net/viewtopic.php?id=13008*)
to changeSubString of t from s to r
set d to text item delimiters
set text item delimiters to s
set t to t's text items
set text item delimiters to r
tell t to set t to beginning & ({""} & rest)
set text item delimiters to d
t
end changeSubString
</nowiki>
on say (message)
(* Log ins EventLog des AppleScript Editors schreiben *)
log {message}
(* Message ins Log auffe Disk schreiben *)
if gDebugLog is true then
tell application "Finder"
set theLine to (do shell script "date +'%Y-%m-%d %H:%M:%S'" as string) & ": " & (message as string)
do shell script "echo " & "'" & theLine & "'" & " >> " & gFileLogPOSIXPath
end tell
end if
end say
on chomp(stringwithspaces)
set cmd to "echo " & stringwithspaces & " | awk '{printf $1}'"
return (do shell script cmd)
end chomp
(* Den Adobe Crash Reporter (oder mehrere davon) entsorgen *)
on killAdobeCrashReporter()
set app_name to "AdobeCrashReporter"
set the_pids to (do shell script "ps ax | grep " & (quoted form of app_name) & " | grep -v grep | awk '{print $1}' | tr ' ' ' '") (* Der Zeilenvorschub ist kein Tippfehler! Ich tausche hier das Return gegen ein Space, damit Kill mehrere Prozesse killen kann *)
if the_pids is not "" then
try
do shell script ("kill -9 " & the_pids)
on error
tell me to say ("Killing AdobeCrashReporter, pid " & the_pids & " failed.")
end try
end if
end killAdobeCrashReporter
(* Das Adobe Photoshop abwuergen *)
on killAdobePhotoshop()
set app_name to "Adobe Photoshop"
set the_pids to (do shell script "ps ax | grep " & (quoted form of app_name) & " | grep -v grep | awk '{print $1}' | tr ' ' ' '") (* Der Zeilenvorschub ist kein Tippfehler! Ich tausche hier das Return gegen ein Space, damit Kill mehrere Prozesse killen kann *)
if the_pids is not "" then
tell me to say ("Killing " & app_name & "!")
try
tell application "Finder"
(* Da es sein kann, dass ich noch eine pid feststellen konnte, aber ID sich zwischenzeitlich doch gracefully verabschiedet, mache ich das mit einem try (Gefahr, dass das Script sonst stehen bleinbt, weil das kill keinen Process findet) *)
do shell script ("kill -9 " & the_pids)
tell me to say ("Killing " & app_name & ", pid " & the_pids & " succeeded.")
end tell
on error
tell me to say ("ERROR: Killing " & app_name & ", pid " & the_pids & " failed.")
end try
else
tell me to say ("Not killing " & app_name & ", because PID is nil (already quit?).")
end if
(* Tempfiles wegwerfen *)
#nur ID tell me to removeRecoveryFiles()
end killAdobePhotoshop
(* *************************************************************************** *)
(* on run/idle handlers *)
(* when starting up... *)
on run
(* Preferences, feel free to change *)
set gSearchFolder to (POSIX file ((POSIX path of (path to home folder)) & "Desktop/tescht/")) (* use trailing "/"! *)
# set gSearchFolder to (POSIX file ((POSIX path of (path to home folder)) & "Desktop/CreateLowRes/Testfiles/")) (* use trailing "/"! *)
set gTargetDPI to 72
set gFolderNameLowRes to "LowRes"
set gDebugLog to true
set gFileSuffixesToProcess to {"jpg", "jpeg", "psd", "tiff", "tif", "bmp", "eps", "png"}
set gTargetSuffix to "psd"
(* Don't change these: *)
set gLocked to false
set gSendReturnMax to 4
(* If folder does not exist, ask user to choose one *)
tell application "Finder"
if (the gSearchFolder exists) is false then
set gSearchFolder to choose folder with prompt "Choose folder to watch:"
end if
end tell
set gFileLogPOSIXPath to (POSIX path of gSearchFolder) & "CreateLowRes.log"
tell me to say ("Logging: " & gDebugLog & ", Logfile: " & gFileLogPOSIXPath)
tell me to say ("Scanning " & gSearchFolder)
(* Run *)
tell me to say ("Starting...")
main(gSearchFolder)
(* Done *)
tell me to say ("Fin...")
end run
(* Handler, der einfach nur troedelt, bis das Script wieder laufen soll. *)
on idle
(* Run *)
tell me to say ("Starting...")
main(gSearchFolder)
(* Done *)
tell me to say ("Fin...")
(* erst spaeter nochmal in diesen Handler reinschaun. *)
return 300 (* Angabe in Sekunden *)
end idle