Help me make AppleScript for this...

Discussion in 'macOS' started by celticpride678, Oct 19, 2009.

  1. celticpride678

    Joined:
    Feb 15, 2009
    Location:
    Boston, MA
    #1
    I have a printer that will no longer scan in Snow Leopard without Preview. I actually like that. However, I find it hard to go into Preview and tell it to begin scanning. Can someone please help me make an AppleScript application where I open it and it tells Preview to begin scanning? Thanks a lot!
     
  2. calderone macrumors 68040

    calderone

    Joined:
    Aug 28, 2009
    Location:
    Seattle
    #2
    Preview doesn't actually have an Applescript dictionary. In the past, you could enable it with a terminal command:

    defaults write /Applications/Preview.app/Contents/Info NSAppleScriptEnabled -bool YES

    This doesn't work in SL. I will try to find out how to turn it on.

    You could use automator in this case.

    I will report back soon.

    EDIT: Also could you post screenshots of the steps you take. I don't have a scanner on hand to test.
     
  3. calderone macrumors 68040

    calderone

    Joined:
    Aug 28, 2009
    Location:
    Seattle
    #3
    I got Applescript to see Preview.app. Attached is the modified Info.plist. Backup the original and drop the new one in.

    Here is a basic framework for the script. From here it will have to be a series of menu click's since Preview doesn't have a dictionary of pre-defined commands.

    Code:
    if appIsRunning("Preview") is false then
    	open "/Applications/Preview.app"
    end if
    
    tell application "Preview"
    	activate
    end tell
    
    
    on appIsRunning(appName)
    	tell application "System Events" to (name of processes) contains appName
    end appIsRunning

    I have taken the script to a level where you would get some use. Like I said before, I don't have a scanner around to runs through all the motions. But I think this should be good enough to get you going. You just have to swap in the correct menu actions.

    Code:
    -- If Preview is not running, run it.
    if appIsRunning("Preview") is false then
    	open "/Applications/Preview.app"
    end if
    
    -- Activate Preview and issue menu clicks. In this case a screen shot of the entire screen.
    tell application "Preview" to activate
    menu_click({"Preview", "File", "Take Screen Shot", "From Entire Screen"})
    
    ----------------------------------------------------------------------------------------------
    -- Functions
    
    -- Checks if an application is running
    on appIsRunning(appName)
    	tell application "System Events" to (name of processes) contains appName
    end appIsRunning
    
    -- `menu_click`, by Jacob Rus, September 2006
    -- 
    -- Accepts a list of form: `{"Finder", "View", "Arrange By", "Date"}`
    -- Execute the specified menu item.  In this case, assuming the Finder 
    -- is the active application, arranging the frontmost folder by date.
    on menu_click(mList)
    	local appName, topMenu, r
    	
    	-- Validate our input
    	if mList's length < 3 then error "Menu list is not long enough"
    	
    	-- Set these variables for clarity and brevity later on
    	set {appName, topMenu} to (items 1 through 2 of mList)
    	set r to (items 3 through (mList's length) of mList)
    	
    	-- This overly-long line calls the menu_recurse function with
    	-- two arguments: r, and a reference to the top-level menu
    	tell application "System Events" to my menu_click_recurse(r, ((process appName)'s ¬
    		(menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))
    end menu_click
    
    on menu_click_recurse(mList, parentObject)
    	local f, r
    	
    	-- `f` = first item, `r` = rest of items
    	set f to item 1 of mList
    	if mList's length > 1 then set r to (items 2 through (mList's length) of mList)
    	
    	-- either actually click the menu item, or recurse again
    	tell application "System Events"
    		if mList's length is 1 then
    			click parentObject's menu item f
    		else
    			my menu_click_recurse(r, (parentObject's (menu item f)'s (menu f)))
    		end if
    	end tell
    end menu_click_recurse
     

    Attached Files:

Share This Page