Skip navigation

Applescript display image

2936 Views 19 Replies Latest reply: May 14, 2012 1:28 PM by red_menace RSS
1 2 Previous Next
derekshull Calculating status...
Currently Being Moderated
May 9, 2012 4:11 AM

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)
  • Camelot Level 8 Level 8 (45,670 points)
    Currently Being Moderated
    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.

  • Camelot Level 8 Level 8 (45,670 points)
    Currently Being Moderated
    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.

  • red_menace Level 6 Level 6 (14,275 points)
    Currently Being Moderated
    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?

  • red_menace Level 6 Level 6 (14,275 points)
    Currently Being Moderated
    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.

  • red_menace Level 6 Level 6 (14,275 points)
    Currently Being Moderated
    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
  • red_menace Level 6 Level 6 (14,275 points)
    Currently Being Moderated
    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.

  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    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 using setReleasedWhenClosed_(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.

1 2 Previous Next

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.