Applescript Finder Selection "Stuck" - applescript

I've been using the code below for awhile to auto-magically scale images down through the various wallpaper sizes that I want. This morning, it ceased working and is giving some of the most bizarre behavior I've ever seen.
If I reboot my box, the script works as expected the first time it runs. After that, the previous selection seems to be stuck in the Image Events memory. What I mean is the following:
I have this directory structure:
.
|-- 11
| `-- Math.png
|-- 1610
|-- 169
| `-- Math.png
|-- 43
| `-- Math.png
|-- Math.jpg
`-- Math.png
I run the script for the first time after rebooting with ./169/Math.png selected in the Finder and get the following results in Applescript Editor:
tell application "Finder"
get selection
--> {document file "Math.png" of folder "169" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk}
get document file "Math.png" of folder "169" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk
--> alias "Paul:Users:tim:Downloads:test:169:Math.png"
end tell
tell application "Image Events"
open alias "Paul:Users:tim:Downloads:test:169:Math.png"
--> image "Math.png"
get dimensions of image "Math.png"
--> {2560, 1440}
scale image "Math.png" to size 1920
get name of image "Math.png"
--> "Math.png"
get name of image "Math.png"
--> "Math.png"
offset of "." in "Math.png"
--> error number -1708
«event ascrgdut»
--> error number -1708
offset of "." in "Math.png"
--> error number -10004
end tell
tell current application
offset of "." in "Math.png"
--> 5
end tell
tell application "Finder"
get parent of document file "Math.png" of folder "169" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk
--> alias "Paul:Users:tim:Downloads:test:169:"
end tell
tell application "Image Events"
save image "Math.png" in "/Users/tim/Downloads/test/169/Math_1920x1080.png" as PNG
--> file "Paul:Users:tim:Downloads:test:169:Math_1920x1080.png"
scale image "Math.png" to size 1366
get name of image "Math.png"
--> "Math.png"
get name of image "Math.png"
--> "Math.png"
offset of "." in "Math.png"
--> error number -10004
end tell
tell current application
offset of "." in "Math.png"
--> 5
end tell
tell application "Finder"
get parent of document file "Math.png" of folder "169" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk
--> alias "Paul:Users:tim:Downloads:test:169:"
end tell
tell application "Image Events"
save image "Math.png" in "/Users/tim/Downloads/test/169/Math_1366x768.png" as PNG
--> file "Paul:Users:tim:Downloads:test:169:Math_1366x768.png"
end tell
Result:
file "Paul:Users:tim:Downloads:test:169:Math_1366x768.png" of application "Image Events"
tell application "Finder"
get selection
--> {document file "Math.png" of folder "169" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk}
get document file "Math.png" of folder "169" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk
--> alias "Paul:Users:tim:Downloads:test:169:Math.png"
end tell
tell application "Image Events"
open alias "Paul:Users:tim:Downloads:test:169:Math.png"
--> image "Math.png"
get dimensions of image "Math.png"
--> {2560, 1440}
scale image "Math.png" to size 1920
get name of image "Math.png"
--> "Math.png"
get name of image "Math.png"
--> "Math.png"
offset of "." in "Math.png"
--> error number -1708
«event ascrgdut»
--> error number -1708
offset of "." in "Math.png"
--> error number -10004
end tell
tell current application
offset of "." in "Math.png"
--> 5
end tell
tell application "Finder"
get parent of document file "Math.png" of folder "169" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk
--> alias "Paul:Users:tim:Downloads:test:169:"
end tell
tell application "Image Events"
save image "Math.png" in "/Users/tim/Downloads/test/169/Math_1920x1080.png" as PNG
--> file "Paul:Users:tim:Downloads:test:169:Math_1920x1080.png"
scale image "Math.png" to size 1366
get name of image "Math.png"
--> "Math.png"
get name of image "Math.png"
--> "Math.png"
offset of "." in "Math.png"
--> error number -10004
end tell
tell current application
offset of "." in "Math.png"
--> 5
end tell
tell application "Finder"
get parent of document file "Math.png" of folder "169" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk
--> alias "Paul:Users:tim:Downloads:test:169:"
end tell
tell application "Image Events"
save image "Math.png" in "/Users/tim/Downloads/test/169/Math_1366x768.png" as PNG
--> file "Paul:Users:tim:Downloads:test:169:Math_1366x768.png"
end tell
Result:
file "Paul:Users:tim:Downloads:test:169:Math_1366x768.png" of application "Image Events"
tell application "Finder"
get selection
--> {document file "Math.png" of folder "169" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk}
get document file "Math.png" of folder "169" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk
--> alias "Paul:Users:tim:Downloads:test:169:Math.png"
end tell
tell application "Image Events"
open alias "Paul:Users:tim:Downloads:test:169:Math.png"
--> image "Math.png"
get dimensions of image "Math.png"
--> {2560, 1440}
scale image "Math.png" to size 1920
get name of image "Math.png"
--> "Math.png"
get name of image "Math.png"
--> "Math.png"
offset of "." in "Math.png"
--> error number -1708
«event ascrgdut»
--> error number -1708
offset of "." in "Math.png"
--> error number -10004
end tell
tell current application
offset of "." in "Math.png"
--> 5
end tell
tell application "Finder"
get parent of document file "Math.png" of folder "169" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk
--> alias "Paul:Users:tim:Downloads:test:169:"
end tell
tell application "Image Events"
save image "Math.png" in "/Users/tim/Downloads/test/169/Math_1920x1080.png" as PNG
--> file "Paul:Users:tim:Downloads:test:169:Math_1920x1080.png"
scale image "Math.png" to size 1366
get name of image "Math.png"
--> "Math.png"
get name of image "Math.png"
--> "Math.png"
offset of "." in "Math.png"
--> error number -10004
end tell
tell current application
tell application "Finder"
get selection
--> {document file "Math.png" of folder "169" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk}
get document file "Math.png" of folder "169" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk
--> alias "Paul:Users:tim:Downloads:test:169:Math.png"
end tell
tell application "Image Events"
open alias "Paul:Users:tim:Downloads:test:169:Math.png"
--> image "Math.png"
get dimensions of image "Math.png"
--> {2560, 1440}
scale image "Math.png" to size 1920
get name of image "Math.png"
--> "Math.png"
get name of image "Math.png"
--> "Math.png"
offset of "." in "Math.png"
--> error number -1708
«event ascrgdut»
--> error number -1708
offset of "." in "Math.png"
--> error number -10004
end tell
tell current application
offset of "." in "Math.png"
--> 5
end tell
tell application "Finder"
get parent of document file "Math.png" of folder "169" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk
--> alias "Paul:Users:tim:Downloads:test:169:"
end tell
tell application "Image Events"
save image "Math.png" in "/Users/tim/Downloads/test/169/Math_1920x1080.png" as PNG
--> file "Paul:Users:tim:Downloads:test:169:Math_1920x1080.png"
scale image "Math.png" to size 1366
get name of image "Math.png"
--> "Math.png"
get name of image "Math.png"
--> "Math.png"
offset of "." in "Math.png"
--> error number -10004
end tell
tell current application
offset of "." in "Math.png"
--> 5
end tell
tell application "Finder"
get parent of document file "Math.png" of folder "169" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk
--> alias "Paul:Users:tim:Downloads:test:169:"
end tell
tell application "Image Events"
save image "Math.png" in "/Users/tim/Downloads/test/169/Math_1366x768.png" as PNG
--> file "Paul:Users:tim:Downloads:test:169:Math_1366x768.png"
end tell
Result:
file "Paul:Users:tim:Downloads:test:169:Math_1366x768.png" of application "Image Events"
I run the script again with ./Math.png selected in Finder:
tell application "Finder"
get selection
--> {document file "Math.png" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk}
get document file "Math.png" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk
--> alias "Paul:Users:tim:Downloads:test:Math.png"
end tell
tell application "Image Events"
open alias "Paul:Users:tim:Downloads:test:Math.png"
--> image "Math.png"
get dimensions of image "Math.png"
--> {2560, 1440}
scale image "Math.png" to size 1920
get name of image "Math.png"
--> "Math.png"
get name of image "Math.png"
--> "Math.png"
offset of "." in "Math.png"
--> error number -10004
end tell
tell current application
offset of "." in "Math.png"
--> 5
end tell
tell application "Finder"
get parent of document file "Math.png" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk
--> alias "Paul:Users:tim:Downloads:test:"
end tell
tell application "Image Events"
save image "Math.png" in "/Users/tim/Downloads/test/Math_1920x1080.png" as PNG
--> file "Paul:Users:tim:Downloads:test:Math_1920x1080.png"
scale image "Math.png" to size 1366
get name of image "Math.png"
--> "Math.png"
get name of image "Math.png"
--> "Math.png"
offset of "." in "Math.png"
--> error number -10004
end tell
tell current application
offset of "." in "Math.png"
--> 5
end tell
tell application "Finder"
get parent of document file "Math.png" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk
--> alias "Paul:Users:tim:Downloads:test:"
end tell
tell application "Image Events"
save image "Math.png" in "/Users/tim/Downloads/test/Math_1366x768.png" as PNG
--> file "Paul:Users:tim:Downloads:test:Math_1366x768.png"
end tell
Result:
file "Paul:Users:tim:Downloads:test:Math_1366x768.png" of application "Image Events"
Notice that the previously selected ./169/Math.png is the one that gets open when I call open currentWallpaper as (alias), despite the fact that the correct wallpaper shows up in the Finder selection. In other words, it's not like Finder is stuck with the old selection, but that the wallpapers that I'm looping through get stuck on the old ones.
Perhaps even more bizarre, is that if I move the old wallpaper, that I had selected, the currentWallpaper reference moves with it! In other words, even if I trash the old selection, the output from the script reports that it's opening the one in ./.Trashes.
It does not seem to get confused as long as the names are different.
The Script:
tell application "Finder" to set wallpapers to selection
set myDimensions to {w169:[{2560, 1440}, {1920, 1080}, {1366, 768}], w1610:[{2560, 1600}, {1920, 1200}, {1440, 900}, {1280, 800}], f43:[{1600, 1200}], s:[{1024, 1024}]}
tell application "Image Events"
repeat with currentWallpaper in wallpapers
set theWallpaper to open (currentWallpaper as alias)
tell theWallpaper
set theWallpaperDimensions to dimensions of theWallpaper
set currentAspects to {}
if my aspectsEqual(16 / 10, theWallpaperDimensions) then set currentAspects to w1610 of myDimensions
if my aspectsEqual(16 / 9, theWallpaperDimensions) then set currentAspects to w169 of myDimensions
if my aspectsEqual(4 / 3, theWallpaperDimensions) then set currentAspects to f43 of myDimensions
if my aspectsEqual(1, theWallpaperDimensions) then set currentAspects to s of myDimensions
repeat with dim in currentAspects
-- Scale and Crop to current dimensions
if item 1 of theWallpaperDimensions is greater than item 1 of dim then
scale to size (get item 1 of dim)
-- Set up save target
set namePrefix to text items 1 thru ((offset of "." in (get name)) - 1) of (get name) as string
tell application "Finder"
set _ to parent of currentWallpaper as alias
set parentPath to POSIX path of _
end tell
set saveTarget to parentPath & namePrefix & "_" & (get item 1 of dim) & "x" & (get item 2 of dim) & ".png"
--save to target
save in saveTarget as PNG
end if
end repeat
end tell
end repeat
end tell
on aspectsEqual(baseAspect, wallpaperDimensions)
set wallpaperAspect to (item 1 of wallpaperDimensions) / (item 2 of wallpaperDimensions)
set aspectDifference to baseAspect - wallpaperAspect
if aspectDifference is less than 0 then set aspectDifference to aspectDifference * -1
return aspectDifference is less than 1.0E-3
end aspectsEqual

The problem is caused by way "Image Events" returns references to opened images:
open alias "Paul:Users:tim:Downloads:test:Math.png"
--> image "Math.png"
open alias "Paul:Users:tim:Downloads:test:169:Math.png"
--> image "Math.png"
The opened image is referenced by name. If you open another image with the same name, the returned reference may occasionally refer to a previously opened image of the same name.
As a work-around add a close every image before you enter the loop that processes the selected images. Furthermore you need to close the opened image when you are done with it:
tell application "Image Events"
close every image
repeat with currentWallpaper in wallpapers
set theWallpaper to open (currentWallpaper as alias)
tell theWallpaper
...
end tell
close theWallpaper
end repeat
end tell

Related

For Mac - How to Rename File to Folder name plus a number thus making the files sequential

Like my title asks how can I set a folder up to rename a new file to its name plus a sequential number. ie. Folder Name - Cats Palying New File Name - Fluffy.jpg. When I add Fluffy.jpg to the folder it would be renamed to Cats Playing000001.jpg next on would be "Cats Playing000002.jpg.
In my research, I found this answer but not sure how to change it to my application. Placing the following script into folder actions:
on adding folder items to the target_folder after receiving these_items
tell application "Finder"
set all_images to every item of the target_folder as list
repeat with i from 1 to the count of these_items --iterates through all the items you dropped in the folder
set this_image to item i of these_items --the current image
set the name of this_image to "image" & (i + the count of all_images) as string --renames the image based on the number of images already in the folder
end repeat
end tell
end adding folder items to

AppleScript find folder with specific file

I have a folder with file /resources/video.mov. This folder with video can located in any place, like /Users/UserName/Desktop/resources/video.mov or /Applications/resources/video.mov and etc. I need to find this folder with video and copy to specific place. I can't find path of folder with video to copy it.
tell application "Finder"
duplicate items in folder "_locatedPath_" to folder "_destiontionPath_" with replacing
end tell
This works for me using the latest version of Sierra
Change the value of variable destinationFolder to the output folder of your choice
property theContainer : missing value
property containingFolder : "resources" -- change if needed
property theSearch : "video.mov" -- change if needed
property destinationFolder : (path to desktop as text) -- change if needed
set findFile to do shell script "mdfind -name " & theSearch
repeat with i from 1 to number of paragraphs in findFile
set this_item to POSIX file (paragraph i of findFile) as alias
tell application "Finder"
if name of this_item is theSearch then
set theContainer to the container of this_item as text
if theContainer contains containingFolder then
set resultObject to duplicate this_item ¬
to destinationFolder ¬
replacing true ¬
routing suppressed true ¬
with exact copy
end if
end if
end tell
end repeat

Reveal files of same size in a folder using Applescript

I'm trying to reveal items of the same size in a folder, and am just testing out the following code, but I keep getting this error:
error "Finder got an error: Can’t get some file of alias \"Macintosh HD:Temp to be Listened to:Temp:Short Temp (Small Files, <20mb):New Folder With Items:\" whose size = \"8890814\" and not 64."
Do you know how I can get the Finder to reveal a file that is the exact same size as (but with a different name to) another file in the same folder? Here's the code I came up with:
tell application "Finder"
set theFolder to choose folder
set noofFiles to number of files in folder theFolder as text
get some file of theFolder
set theFile to result
print result
display notification name of theFile as text
delay 1
get size of theFile as text
set theSize to result
get (some file of theFolder whose size is equal to theSize and name does not contain theFile)
reveal the result
end tell
Thanks
Tardy
You cannot compare an Finder file specifier (theFile) with a string (name)
set fileName to name of theFile
get (some file of theFolder whose size is equal to theSize and name does not contain fileName).
Side-note: Using result all the time is dangerous. It contains always the result of the preceding line. If that line has no result, it contains nothing.

get all folders in a files path in Applescript - so i can tag them

I have a applescript that gets me a file object
I now want to walk through all of the folders above it in its path (each file could be many levels down) and tag them a colour.
So far I can see there is a path in file, but I dont know what type to cast it to (its not a string):
copy path of theFile as string to FileNamesPath
If I could get each folder, I could then apply a tag/label to make them a colour:
tell application "Finder" to set label index of theFolder to 3
How do I get each folder from the file?
To get the parent folder i use the container property.
For example we get for an file on the desktop:
-> folder "Desktop" of folder "USERNAME" of folder "Users" of startup disk
To work with this output i change it to an alias:
-> alias "Macintosh HD:Users:USERNAME:Desktop"
With a repeat loop i get all parent folders. To stop i you a stopFolder variable. In this case the user folder.
tell application "Finder"
set stopFolder to POSIX path of the (path to the users folder)
set labelFolder to (choose file)
set lastParentFolder to (container of labelFolder as alias)
repeat
if POSIX path of lastParentFolder = stopFolder then return "ready"
label index of lastParentFolder # get label index
set label index of lastParentFolder to 3 # set new label index
set lastParentFolder to (container of lastParentFolder as alias)
end repeat
end tell
The
get label index
row is only to show the original label index in the protocol.
If you have a file or alias object with HFS path like this
alias "Macintosh HD:Users:user:Images:1401711772700.jpg" or
file "Macintosh HD:Users:user:Images:1401711772700.jpg"
you can try this little script
tell application "Finder"
set labelfolder to (choose file) #or insert file object here
repeat
set labelfolder to container of labelfolder
if labelfolder is startup disk then exit repeat
try #labels are disabled on some folders
set label index of labelfolder to 3
end try
end repeat
end tell

Folder Action to resize images in Leopard 10.5.8

I know nothing about AppleScript, so please forgive me.
Goal is to create a folder that our graphics guy can drop stock photos into and have them resized to 1280. The chosen folder is a network folder, so don't know if that's part of the issue or not. He will be pasting the images into the folder from his Windows workstation.
Here is what I have pieced together so far:
on adding folder items to this_folder after receiving added_items
repeat with this_item in added_items
try
rescale_and_save(this_item)
end try
end repeat
end adding folder items to
to rescale_and_save(this_item)
tell application "Image Events"
launch
set the target_height to 1280
-- open the image file
set this_image to open this_item
set typ to this_image's file type
copy dimensions of this_image to {current_width, current_height}
if current_height is greater than current_width then
scale this_image to size target_height
else
-- figure out new height
-- y2 = (y1 * x2) / x1
set the new_width to (current_width * target_height) / current_height
scale this_image to size new_width
end if
tell application "Finder" to set new_item to ¬
(container of this_item as string) & "ex" & (name of this_item)
save this_image in new_item as typ
end tell
end rescale_and_save
I've right clicked on the folder, enabled folder actions, and added my script. I copy in the test file but nothing happens.
Any ideas?
EDIT:
I looked at the samples and came up with the following script that WORKS!:
property done_foldername : "Scaled Images"
property target_height : 1280
-- the list of file types which will be processed
-- eg: {"PICT", "JPEG", "TIFF", "GIFf"}
property type_list : {"JPEG", "TIFF", "PNGf"}
-- since file types are optional in Mac OS X,
-- check the name extension if there is no file type
-- NOTE: do not use periods (.) with the items in the name extensions list
-- eg: {"txt", "text", "jpg", "jpeg"}, NOT: {".txt", ".text", ".jpg", ".jpeg"}
property extension_list : {"jpg", "jpeg", "tif", "tiff", "png"}
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
tell application "Finder"
if not (exists folder done_foldername of this_folder) then
make new folder at this_folder with properties {name:done_foldername}
set current view of container window of this_folder to list view
end if
set the target_folder to folder done_foldername of this_folder
end tell
-- PROCESS EACH OF THE ITEMS ADDED TO THE ATTACHED FOLDER
try
repeat with i from 1 to number of items in these_items
set this_item to item i of these_items
set the item_info to the info for this_item
-- CHECK TO SEE IF THE ITEM IS AN IMAGE FILE OF THE ACCEPTED FILE TYPE
if (alias of the item_info is false and the file type of the item_info is in the type_list) or (the name extension of the item_info is in the extension_list) then
tell application "Finder"
-- LOOK FOR EXISTING MATCHING ITEMS IN THE DESTINATION FOLDER
-- IF THERE ARE MATCHES, THEN RENAME THE CONFLICTING FILES INCREMENTALLY
my resolve_conflicts(this_item, target_folder)
-- MOVE THE ITEM TO THE DESTINATION FOLDER
set the target_file to (move this_item to the target_folder with replacing) as alias
end tell
-- PROCESS THE ITEM
process_item(target_file)
end if
end repeat
on error error_message number error_number
if the error_number is not -128 then
tell application "Finder"
activate
display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
end tell
end if
end try
end adding folder items to
on resolve_conflicts(this_item, target_folder)
tell application "Finder"
set the file_name to the name of this_item
if (exists document file file_name of target_folder) then
set file_extension to the name extension of this_item
if the file_extension is "" then
set the trimmed_name to the file_name
else
set the trimmed_name to text 1 thru -((length of file_extension) + 2) of the file_name
end if
set the name_increment to 1
repeat
set the new_name to (the trimmed_name & "." & (name_increment as string) & "." & file_extension) as string
if not (exists document file new_name of the target_folder) then
-- rename to conflicting file
set the name of document file file_name of the target_folder to the new_name
exit repeat
else
set the name_increment to the name_increment + 1
end if
end repeat
end if
end tell
end resolve_conflicts
-- this sub-routine processes files
on process_item(this_item)
-- NOTE that the variable this_item is a file reference in alias format
-- FILE PROCESSING STATEMENTS GOES HERE
try
-- convert alias reference to string
set this_item to this_item as string
with timeout of 900 seconds
tell application "Image Events"
launch -- always use with Folder Actions
set this_image to open file this_item
scale this_image to size target_height
save this_image with icon
close this_image
end tell
end timeout
on error error_message
tell application "Finder"
activate
display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
end tell
end try
end process_item
I just tested your script on a folder dropping 1 image in, and it turns out it runs repeatedly because the resulting (resized) image is also added to that folder. Here's what it looked like after a couple of seconds, before I interrupted it:
You should have a look at the default image processing scripts inside /Library/Scripts/Folder Action Scripts and maybe use one of them as a template. They basically use a sub-folder for the processed images and contain some possibly useful filename conflict resolving function, too.

Resources