As an exercise I built this multipurpose folder actions script :
--set this_folder to ((path to desktop as text)&"hotFolder:") as alias
--set these_items to ((path to desktop as text)&"19932.jpg") as alias}
on adding folder items to this_folder after receiving these_items
-- CHECK FOR THE DESTINATION FOLDER WITHIN THE ATTACHED FOLDER
-- IF IT DOESN'T EXIST, THEN CREATE IT
repeat with anItem in these_items # these_items is a list of aliases
set itsInfos to info for anItem
set {isFolder, isPackage} to {folder, package folder} of itsInfos
if isFolder and (not isPackage) then
set itsID to "folder" # fake type identifier
else
set itsID to type identifier of itsInfos
end if
-- some samples
-- {true, false, "folder"} for a folder
-- {false, false, "com.apple.applescript.script"}
-- {false, false, "com.adobe.pdf"}
-- {false, false, "public.jpeg"}
-- {false, false, "public.png"}
-- {false, false, "public.tiff"}
-- {false, false, "com.apple.iwork.pages.sffpages"}
-- {false, false, "com.apple.iwork.pages.pages"}
-- {false, false, "com.apple.iwork.numbers.sffnumbers"}
-- {true, true, "com.apple.iwork.numbers.numbers"}
-- {true, true, "com.apple.rtfd"}
-- {true, true, "com.apple.application-bundle"
if itsID = "folder" then
my treatThisFolder(anItem, this_folder)
else if itsID is "com.adobe.pdf" then
my treatPDF(anItem, this_folder)
else if itsID starts with "com.apple.iwork" then
my treatIworkDoc(anItem, this_folder, itsID)
else if itsID is in {"public.jpeg", "public.png", "public.tiff"} then # you may add other IDs
my treatPicture(anItem, this_folder, itsID)
else if itsID is "?!?!" then
--
end if
end repeat
end adding folder items to
on treatThisFolder(sourceFolder, this_folder)
set stopCharacters to "_X"
set stopCharacterCount to (count stopCharacters)
tell application "Finder"
set sourceFiles to (every file in sourceFolder ¬
whose name contains stopCharacters and name ends with "pdf") as alias list
end tell
repeat with aFile in sourceFiles
tell application "Finder" to set aFileName to name of aFile
set stopOffset to (offset of stopCharacters in aFileName) + stopCharacterCount - 1
set newFileName to text 1 thru stopOffset of aFileName & ".pdf"
tell application "Finder"
try
set name of aFile to newFileName
on error errorMessage
set dialogText to "The source file " & quote & aFileName & quote & ¬
" could not be renamed. The error message was:" & return & return & errorMessage
display dialog dialogText buttons {"Cancel", "Skip"} default button 1
end try
end tell
end repeat
end treatThisFolder
on treatPDF(anItem, this_folder)
set targetFolder to my buildFolder(this_folder, "treated PDFs")
# now we may move the PDF in to targetFolder and treat the moved file
end treatPDF
on treatIworkDoc(anItem, this_folder, itsID)
set targetFolder to my buildFolder(this_folder, "treated iWork docs")
# itsID allow us to select the correct tratment to apply
# now we may move the iWork doc in to targetFolder and treat the moved file
end treatIworkDoc
on treatPicture(anItem, this_folder, itsID)
set targetFolder to my buildFolder(this_folder, "treated Pictures")
# itsID allow us to select the correct tratment to apply
# now we may move the picture file in to targetFolder and treat the moved file
end treatPicture
on buildFolder(this_folder, folderName)
set destinationFolder to (this_folder as text) & folderName & ":"
tell application "Finder"
if not (exists folder destinationFolder) then
make new folder at this_folder with properties {name:folderName}
end if
end tell
return destinationFolder as alias
end buildFolder
The two first instructions (enabled) are used to test the script after disabling the instructions:
[format]on adding folder items to this_folder after receiving these_items
end adding folder items to[/format]
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 20 aout 2019 11:21:18