I have used these “on open” scripts for years to streamline file management tasks. For some reason lately I am getting this problem where the “on open” is not adding all items to the list it creates.
Here you can see I am clearly dropping 8 AIs but it only counts 4.
On top of this, these 8 AIs are 2 sets of 4. The 2nd set has a “copy” appended to the name (and modified slightly). If I drop the 2 sets separately, they work just like normal.
I am sure full of other errors but it has worked for years until this issue. I am not a professional coder, I just hack together what works until it doesn’t, then I get stuck.
global save_folder
global yes_count
global mult
global trimpixels
on open these_items
display dialog (count these_items) -- this is the count making me believe "on open" is not working correctly
set mult to choose from list {1, 2, 3, 4, 5} with prompt "Size multiplier:" default items 1
set trimpixels to (button returned of (display dialog "Trim image?" buttons {"Yes", "No"} default button 2))
tell application "Finder"
-- save folder location
set this_folder to (container of (item 1 of these_items)) as string
set parent_folder to (container of (alias this_folder)) as string
set parent_folder to parent_folder as alias
try
tell me to set save_folder to (choose folder with prompt "Choose the PSD folder." default location parent_folder) as string
set parent_folder to save_folder as alias
set parent_folder to (container of alias save_folder) as string
set parent_folder to parent_folder as alias
on error err
display dialog err
return -- end program
end try
end tell
-- set some variables
set yes_count to 0
set file_count to count of these_items
-- main process loop
repeat with this_item in these_items
tell application "Finder" to set this_ext to name extension of this_item as string
if this_ext is "pdf" then
set count_pages to (do shell script "mdls -name kMDItemNumberOfPages " & quoted form of POSIX path of this_item & " | /usr/bin/grep -o '[0-9]\\+$'") as integer
else
set count_pages to 1
end if
repeat with i from 1 to count_pages
my process_item(this_item as string, i)
end repeat
end repeat
-- display success
tell me to activate
set wrong to ""
if yes_count is less than file_count then set wrong to "Something went wrong." & return & return
tell me to display dialog wrong & "Dropped " & file_count & " files, and processed " & yes_count & "." giving up after 10
end open
on process_item(theFile, theCount)
tell application "Adobe Photoshop 2022"
try
open file theFile with options {class:PDF open options, page:theCount, bits per channel:eight, mode:RGB, crop page:media box, resolution:(mult * 300)} showing dialogs never
on error
display dialog "File not a PDF." giving up after 3
return
end try
set docRef to current document
set docname to (name of docRef)
resize image docRef resolution 300 resample method none
if trimpixels is "Yes" then trim docRef basing trim on transparent pixels
set psdOptions to ¬
{class:Photoshop save options, embed color profile:true, save alpha channels:false, save annotations:false, save layers:true}
save docRef in file (save_folder & docname) as Photoshop format with options psdOptions appending lowercase extension with copying
-- close/end!
close docRef saving no
set yes_count to (yes_count + 1)
end tell
end process_item
Would be interested to know more about the files you are dragging or opening.
And are they on an HFS+ or APFS volume?
Full names of file would also be helpful.
Picture crops off filenames
The “copy” files are saved by me, but the originals are saved out elsewhere. I just tried opening and resaving the originals with no other changes, and they work. This is a big pain, and I don’t understand why it matters. At least it’s not bad code, but I don’t understand why the script would not recognize files saved out elsewhere.
OK, I figured out how to remove the “com.apple.quarantine” inside of Applescript.
on open these_items
tell application "Finder"
-- save folder location
set this_folder to (container of (item 1 of these_items)) as string
set posix_folder to quoted form of POSIX path of this_folder
end tell
do shell script "xattr -rd com.apple.quarantine " & posix_folder
This successfully removes the quarantine from the entire folder containing the files, but I am not able to access the list of dropped files in full. The originally quarantined files are still excluded.
I found this post with code that looks really cryptic. I don’t understand any of this code. Could someone that understands it explain so I can implement? Can it be simplified (this post is 5 years old):