Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript display image

I have a weather map image online that I'd like to have an applescript pull up.



Basically I just want it to pull up in a dialog box maybe or something like that. Is that possible to do?

Mac OS X (10.7.1)

Posted on May 8, 2012 10:33 PM

Reply
19 replies

May 9, 2012 9:55 AM in response to derekshull

AppleScript can't do this by itself, but there are a couple of options.


One is that you can tell another application (e.g. Preview.app) to display the image.

Another is to develop your application in XCode where you can use AppleScriptObjC to build a full-blown application with whatever user interface elements you like, although that's a bigger task.

May 10, 2012 10:18 AM in response to derekshull

Riddle me this:


What if we used a shell script to get the url image and then had applescript trigger the shell script? Is that possible? I tried using the below shell script and then having the second script trigger it but I get a permission denied alert.


shell script:

content=$(wget google.com -q -O -)echo $content


applescript:

do shell script "/path/to/yourscript.sh"

May 10, 2012 11:15 AM in response to derekshull

There's no need to overcomplicate things - you can stuff your wget command directly in your AppleScript:


(here I use curl since wget is not standard on Mac OS X, but the principle is the same)


do shell script "/usr/bin/curl http://www.google.com/"


but that's not your issue. This will return the appropriate URL data back to your script, but it doesn't do anything about displaying it, which was your original request. As I said before, AppleScript has no built-in mechanism to display image data. You could save that image to a file and open it in some other app, but you can't display it solely within your app unless you take the leap to XCode/AppleScriptObjC.

May 10, 2012 11:38 AM in response to derekshull

You will still need to create a place to put the image - that is where the other application (yours or someone elses) comes in. Lion also has a Cocoa-AppleScript Applet template that can be used instead of Xcode, although you would have to create the user interface items (the window, image view, etc) manually. If you are still looking at creating your own application, it would be run like any other. Is this a regular image, or a web page such as your example Google link?

May 10, 2012 12:37 PM in response to derekshull

In the AppleScript Editor, you can create a new Cocoa-AppleScript Applet from the default template by using the File > New from Template menu item. After giving it a name and a save location, the main script of the applet will open in the editor. The default script doesn't have anything in it but some comments, so add the following:


(* 
This is the main script for a Cocoa-AppleScript applet example using an image view.
Add the following handler to the CocoaAppletAppDelegate.scpt (in the application bundle)
for the application to also quit after the window is closed:

on applicationShouldTerminateAfterLastWindowClosed_(sender)
return true
end applicationShouldTerminateAfterLastWindowClosed_
*)
property dialogWindow : missing value -- the window
property dialogImageView : missing value -- the view
property imageURL : "http://i.imwx.com//web/radar/us_lit_closeradar_plus_usen.jpg" -- the image
on run

try
# create the window
set dialogWindow to current application's NSWindow's alloc's initWithContentRect_styleMask_backing_defer_({{200, 600}, {602, 407}}, 7, current application's NSBackingStoreBuffered, true) -- mask of 7 = title, close, minimize

tell dialogWindow -- set some window properties
setPreventsApplicationTerminationWhenModal_(false)
setAllowsConcurrentViewDrawing_(true)
setReleasedWhenClosed_(false)
setLevel_(current application's NSNormalWindowLevel)
setHasShadow_(true)
|center|()

# create an image view
set dialogImageView to current application's NSImageView's alloc's initWithFrame_({{1, 1}, {600, 405}})
it's contentView's addSubview_(dialogImageView) -- add it

# set the contents of the image view
set theURL to current application's NSURL's URLWithString_(imageURL)
set theImage to current application's NSImage's alloc's initWithContentsOfURL_(theURL)
theImage's setSize_({600, 405})
dialogImageView's setImage_(theImage)

end tell

tell dialogWindow to makeKeyAndOrderFront_(me) -- show the window

on error errmess number errnum
log errmess -- just log it to the console
error errmess number errnum -- pass it on
end try

end run


I set the size of the dialog to the size of the image (with a 1 pixel border). Note that editing a Cocoa-AppleScript application (yes, I know you are going to play with it) is a bit more of a pain, in that you can't run the script directly from the editor - you will need to save the changes and then run the application. Also be aware that the application won't quit if there is a problem - actually, it doesn't quit at all unless you tell it to.

May 10, 2012 1:10 PM in response to derekshull

Cocoa applications are a bit different than regular AppleScripts - in order to keep the UI responsive (to quit it via the menu, for example), you need to periodically handle events (mouse and key presses, etc) to keep them from backing up in the queue. You can use the following handler (from Shane Stanley, http://macosxautomation.com/applescript/apps/) to do this - I've noticed lately that a regular delay command is also a bit squirrelly, so you can modify the previous script as follows:


First, add a handler (after the run handler) to fetch events and pass them on:


on fetchEvents() -- handle user events to keep the queue from filling up (Shane Stanley)
repeat -- forever
tell current application's NSApp to set theEvent to nextEventMatchingMask_untilDate_inMode_dequeue_(current application's NSAnyEventMask, missing value, current application's NSEventTrackingRunLoopMode, true)
if theEvent is missing value then -- none left
exit repeat
else
tell current application's NSApp to sendEvent_(theEvent) -- pass it on
end if
end repeat
return
end fetchEvents



Next, add a few statements to the end of the run handler (in my previous post) to use a different kind of delay and call the above handler:


   repeat 40 times -- 20 seconds
current application's NSThread's sleepForTimeInterval_(0.5)
fetchEvents()
end repeat

tell me to quit

May 10, 2012 9:18 PM in response to red_menace

So I did what you said and it worked once and hasn't worked since that one time :-/


here's what I have:



-- main.scpt

-- Cocoa-AppleScript Applet

--

-- Copyright 2011 {Your Company}. All rights reserved.


-- This is the main script for a Cocoa-AppleScript Applet.

-- You can put the usual script applet handlers here.


--Script by red_menace from Apple Discussions


(*

This is the main script for a Cocoa-AppleScript applet example using an image view.

Add the following handler to the CocoaAppletAppDelegate.scpt (in the application bundle)

for the application to also quit after the window is closed:

on applicationShouldTerminateAfterLastWindowClosed_(sender)

return true

end applicationShouldTerminateAfterLastWindowClosed_

*)


on fetchEvents() -- handle user events to keep the queue from filling up (Shane Stanley)

repeat -- forever

tell current application's NSApp to set theEvent to nextEventMatchingMask_untilDate_inMode_dequeue_(current application's NSAnyEventMask, missing value, current application's NSEventTrackingRunLoopMode, true)

if theEvent is missing value then -- none left

exit repeat

else

tell current application's NSApp to sendEvent_(theEvent) -- pass it on

end if

end repeat

return

end fetchEvents


property dialogWindow : missing value -- the window

property dialogImageView : missing value-- the view

property imageURL : "http://i.imwx.com//web/radar/us_lit_closeradar_plus_usen.jpg" -- the image



on run


try


# create the window

set dialogWindow to current application's NSWindow's alloc's initWithContentRect_styleMask_backing_defer_({{200, 600}, {602, 407}}, 7, current application's NSBackingStoreBuffered, true) -- mask of 7 = title, close, minimize


tell dialogWindow-- set some window properties


setPreventsApplicationTerminationWhenModal_(false)


setAllowsConcurrentViewDrawing_(true)


setReleasedWhenClosed_(false)


setLevel_(current application's NSNormalWindowLevel)


setHasShadow_(true)


|center|()



# create an image view

set dialogImageView to current application's NSImageView's alloc's initWithFrame_({{1, 1}, {600, 405}})

it's contentView's addSubview_(dialogImageView) -- add it



# set the contents of the image view

set theURL to current application's NSURL's URLWithString_(imageURL)

set theImage to current application's NSImage's alloc's initWithContentsOfURL_(theURL)

theImage's setSize_({600, 405})


dialogImageView's setImage_(theImage)


end tell


tell dialogWindow to makeKeyAndOrderFront_(me) -- show the window


on error errmessnumbererrnum


logerrmess-- just log it to the console

error errmessnumbererrnum-- pass it on

end try


repeat 40 times -- 20 seconds


current application's NSThread's sleepForTimeInterval_(0.5)


fetchEvents()

end repeat


tell me to quit


end run




like i said it worked once then just quit on me :-/

May 10, 2012 9:30 PM in response to derekshull

spoke a little soon...it's up it's just acting weird.


when I pull it up for the first time it brings it to me fine but then I quit it and run the app again and the app will run but I have to switch to another application like chrome or mail or something and then switch back to the weather map app to get it to pull up. Anyone know why? Beautifully done though I might add :-) thanks a bunch!

May 11, 2012 5:49 AM in response to derekshull

I've been playing with the application a bit to try and find a better way for the delay (and add a preference to keep track of the last window position), and it seems to work OK for me. As I noted before, if there is an error the application will not quit, so take a look in the Dock to make sure there isn't an earlier instance still running. You can also look at the Console application to see if any errors have been logged there.

May 11, 2012 8:51 AM in response to red_menace

This is the craziest thing. So I made sure that all the other instances quit and I try to pull it up but it won't show unless I run the script, switch to another application, then switch back to the weather map application. Then it will show. :-/ I've tried to see why it's doing this but no luck in making it just come up on its own...is there a probelm with it coming up to the front?

May 11, 2012 2:30 PM in response to derekshull

I can't boot into my 10.7 partition at the moment to test this, but try usingsetReleasedWhenClosed_(true) instead of false. If there's an unreleased allocation of the window persisting for some reason, that might very well cause confusions. (also, don't you want to be using a panel here rather than a window?)


are you seeing any errors appear in the logs? It's hard to diagnose problems when your whole script is cased in try blocks - you need to see if (and what) errors are getting thrown.

May 12, 2012 3:55 PM in response to derekshull

I can't think of anything that would keep the window from showing up, unless there is an issue with getting the iimage. I've added a check for that in the latest version, which has a better delay method and also keeps track of the location of the window:


(* 
This is the main script for a Cocoa-AppleScript applet example using an image view.
The window and image view frames are sized based on the size of the image.
The location of the window is saved in the preferences if the application quits normally.

Add the following handler to the CocoaAppletAppDelegate.scpt (in the application bundle)
to have the application also quit after the window is closed:

on applicationShouldTerminateAfterLastWindowClosed_(sender)
return true
end applicationShouldTerminateAfterLastWindowClosed_
*)
script MainScript

property dialogWindow : missing value -- this will be the window
property dialogImageView : missing value -- this will be the view
property imageURL : "http://i.imwx.com//web/radar/us_lit_closeradar_plus_usen.jpg" -- the image
property magnification : 1 -- image magnification
property windowLocation : {300, 200} -- initial window location


on main()
try
# set up and read preferences
tell standardUserDefaults() of current application's NSUserDefaults
registerDefaults_({windowLocation:windowLocation}) -- register initial defaults
set windowLocation to objectForKey_("windowLocation") as list -- read existing defaults, if any
end tell

createWindow()

tell dialogWindow
setFrameOrigin_(windowLocation) -- set the location to previous
makeKeyAndOrderFront_(me) -- show the window
end tell

performSelector_withObject_afterDelay_("finish", missing value, 20) -- quit after delay

on error errmess number errnum -- oops
log errmess -- just log the error to the console
error errmess number errnum -- pass the error on
end try
end main


on finish() -- clean up and quit
tell standardUserDefaults() of current application's NSUserDefaults to setObject_forKey_((origin of (dialogWindow's frame() as record)) as list, "windowLocation") -- save current window location
tell me to quit
end finish


on createWindow() -- create the window and image view

# get the image and its size to set the containing views
set theURL to current application's NSURL's URLWithString_(imageURL)
tell current application's NSImage's alloc's initWithContentsOfURL_(theURL) -- create image object
set theImage to it
if theImage is missing value then error "The image URL was not found."
set {theHeight, theWidth} to its |size|() as list -- get image size
# should probably do some checks for minimum/maximum size
set {theHeight, theWidth} to {theHeight * magnification, theWidth * magnification}
its setSize_({theHeight, theWidth}) -- set size for view
end tell

# create the window
tell current application's NSWindow's alloc's initWithContentRect_styleMask_backing_defer_({{200, 600}, {theHeight + 2, theWidth + 2}}, 7, current application's NSBackingStoreBuffered, true) -- mask of 7 = title, close, minimize
set dialogWindow to it
setPreventsApplicationTerminationWhenModal_(false)
setAllowsConcurrentViewDrawing_(true)
setReleasedWhenClosed_(false)
setLevel_(current application's NSNormalWindowLevel)
setHasShadow_(true)

# create the image view
tell current application's NSImageView's alloc's initWithFrame_({{1, 1}, {theHeight, theWidth}})
set dialogImageView to it
dialogImageView's setImage_(theImage)
end tell
its contentView's addSubview_(dialogImageView) -- add the view
end tell

return
end createWindow

end script
on run
MainScript's main()
end run

Applescript display image

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.