Could someone familiar with Applescript kindly tell me how to script the following action so that I could re-use it whenever needed?
When I do this action manually here are the steps:
In Preview,
Open the image to be adjusted
Drag the lower right corner of the image to the lower right corner of the screen
Menu “Tools” Choose adjust size
“Fit into” Choose 1920x1080 pixels
Click OK
Menu “File” Choose Save
tell application "Preview"
activate
end tell
tell application "Finder"
set screenSize to bounds of window of desktop
set screenWidth to item 3 of screenSize
set screenHeight to item 4 of screenSize
end tell
tell application "System Events"
set myFrontMost to name of first item of ¬
(processes whose frontmost is true)
end tell
try
tell application myFrontMost
set bounds of window 1 to {¬
0, ¬
0, ¬
screenWidth, ¬
screenHeight}
end tell
end try
The second try worked and the script compiled correctly. I saved it as an application, but when I tried it on an image nothing happened. I clicked Run without effect. The beginning of the script works, since it opened Preview, but nothing more.
Sorry to bother you again! Can you suggest what went wrong?
I noticed that System Events was not listed in SysPref > Accounts > Login Items.
So I went to HD/system/library/core services/System Events, and added it to the Login Items.
Then I tried again the script on a photo, but nothing happened.
Following your advice, Stefan, I removed System Events from the Log in Items. Thanks
haolesurferdude, The advices in http://www.macosxautomation.com/maveric . index.html apply to Lion. I have Snow Leopard. Also, I don’t have Maverick. Is it required?
I tried your script in the Editor One parag. at a time, but I got the same reaction:
(Syntax Error Expected "end" or "end tell" but found unknown token),
haolesurferdude, When your script wouldn’t compile after I copy-pasted it to the Editor, I tried copying it longhand, and it compiled (??!!!)
I saved it as an app.
Why don’t you move the activate Preview tell block below the System Events tell block? Then, you don’t need to check first process whose frontmost is true.
Edited: I was thinking that if you’re running Mavericks, then note that not all apps become frontmost if it wasn’t running when you ‘activate’. i.e. the ‘activate’ command doesn’t work the same as in previous os’s. Some apps need something like launch AND activate to become frontmost. ‘activate’ works with Preview.
I take that back what I wrote! Preview does not become frontmost in Mvericks with the activate command if it is not already running. Ran this in Script Editor:
If you want to go beyond opening the picture in Preview, Shane Stanley posted several scripts on working with pictures. Here’s one I found in my library to crop I think:
set thisFile to POSIX path of "Macintosh HD:Users:TL:desktop:Ski Images:whitefishT.jpg"
set exportFile to POSIX path of "Macintosh HD:Users:TL:desktop:Ski Images:whitefish.jpg"
set theImage to current application's NSImage's alloc()'s initWithContentsOfFile:thisFile
set theSize to (theImage's |size|()) as record
set theHeight to height of theSize
set theWidth to width of theSize
set newRect to {{x:theWidth / 4, y:theHeight / 4}, {width:theWidth / 2, height:theHeight / 2}}
theImage's lockFocus()
set theRep to current application's NSBitmapImageRep's alloc()'s initWithFocusedViewRect:newRect
theImage's unlockFocus()
set theData to theRep's representationUsingType:(current application's NSJPEGFileType) |properties|:{NSImageCompressionFactor:0.9}
theData's writeToFile:exportFile atomically:true
It looks like you can resize also. I need to review NSImage. You would eliminate Preview altogether.
Edited: come to think of it, it may have been DJ Bazzie Wazzie’s post. Not sure.
Edited: this is the one I modified I think. It’s interesting trying to remember what was happening.
use framework "Foundation"
-- (x1, y1) is origin upper left
on cropTo:{x1, y1, x2, y2} fromPath:inputPath toPath:outputPath
set newWidth to x2 - x1
set newHeight to y2 - y1
set theImage to current application's NSImage's alloc()'s initWithContentsOfFile:inputPath
set theSize to (theImage's |size|()) as record
set oldHeight to height of theSize
-- transpose y value for Cocoa coordintates
set y1 to oldHeight - newHeight - y1
set newRect to {{x:x1, y:y1}, {width:newWidth, height:newHeight}}
theImage's lockFocus()
set theRep to current application's NSBitmapImageRep's alloc()'s initWithFocusedViewRect:newRect
theImage's unlockFocus()
set theData to theRep's representationUsingType:(current application's NSPNGFileType) |properties|:{NSImageGamma:1.0}
theData's writeToFile:outputPath atomically:true
end cropTo:fromPath:toPath:
use framework "Foundation"
-- (x1, y1) is origin upper left
on resizeTo:{x, y} fromPath:inputPath toPath:outputPath
set theImage to current application's NSImage's alloc()'s initWithContentsOfFile:inputPath
theImage's setSize:{width:1440, height:900}
--set oldHeight to height of theSize
-- transpose y value for Cocoa coordintates
--set y1 to oldHeight - newHeight - y1
set newRect to {{x:0, y:0}, {width:1440, height:900}}
theImage's lockFocus()
set theRep to current application's NSBitmapImageRep's alloc()'s initWithFocusedViewRect:newRect
theImage's unlockFocus()
set theData to theRep's representationUsingType:(current application's NSPNGFileType) |properties|:{NSImageGamma:1.0}
theData's writeToFile:outputPath atomically:true
end resizeTo:fromPath:toPath:
This script runs the library script:
use resizeScript : script "ResizeFileToFilelib"
use scripting additions
-- choose the file to crop
set f to (choose file)
set pp to POSIX path of f
-- create file specification for new file
set desk_path to POSIX path of (path to desktop)
set fs to desk_path & "NewFile.png"
-- {x1, y1, x2, y2} origin is upper left
resizeScript's resizeTo:{1440, 900} fromPath:(pp) toPath:(fs)
Still working on it, so disregard the old comments in both scripts.
use framework "Foundation"
-- Resizes input file and creates a new png output file.
-- Not proportional. Adjust width and height before calling.
on resizeTo:{w, h} fromPath:inputPath toPath:outputPath
set theImage to current application's NSImage's alloc()'s initWithContentsOfFile:inputPath
theImage's setSize:{width:w, height:h}
set newRect to {{x:0, y:0}, {width:w, height:h}}
theImage's lockFocus()
set theRep to current application's NSBitmapImageRep's alloc()'s initWithFocusedViewRect:newRect
theImage's unlockFocus()
set theData to theRep's representationUsingType:(current application's NSPNGFileType) |properties|:{NSImageGamma:1.0}
theData's writeToFile:outputPath atomically:true
return true
end resizeTo:fromPath:toPath:
I couldn’t find anything to add proportion. You need to scale the input size.
Kel!,
I am dazzled by your keenness and enthusiasm and thank you for all the trouble you took. However for an Applescript ignorant like myself what I need is a fiable script that will enlarge a jpeg image to the height of my screen, remembering that I am on Snow Leopard.
Haolesurfer dude offered what looked like a workable script but the Compiler objected on a technical point. I reported this but no further help was forthcoming.
It is a pity that I am stuck without knowing who else I could ask to help.
If someone could empathize with this problem I should be most grateful.