Applescript handler help

Discussion in 'Mac Programming' started by starjohn, May 14, 2015.

  1. starjohn macrumors newbie

    Joined:
    Dec 2, 2014
    #1
    Hi Guys,

    I am using MacIntosh 10.8 (Mountain Lion) and Applescript Editor 2.5. I have tried to put 2 set of script in 2 handlers and to use it through If then condition. But it throw an error "The variable desktopTestFolder is not defined." Please let me know where I am wrong.

    Script for your reference:

    Code:
    tell application "Finder"
    	if not (exists folder "Test" of desktop) then make new folder at desktop with properties {name:"Test"}
    end tell
    set desktopTestFolder to (path to desktop folder as text) & "Test:"
    set mychoice to (choose from list {"PS List", "AA Table", "PS Legend", "PO Chart", "MD"} with prompt "Please select which sound you like best" default items "None" OK button name {"Play"} cancel button name {"Cancel"})
    if mychoice is false then error number -128 -- user canceled
    
    set mychoice to mychoice as text
    if mychoice is equal to "PS List" then
    	handler1()
    else
    	handler2()
    end if
    on handler1()
    	tell application "Microsoft Word"
    		activate
    		set theContent to content of text object of selection
    		copy object text object of selection
    		set newDoc to make new document
    		delay 2
    		tell application "System Events"
    			tell process "Microsoft Word"
    				keystroke "v" using command down
    			end tell
    		end tell
    		run VB macro macro name "Normal.NewMacros.Clean"
    		run VB macro macro name "Normal.Module9.bold"
    		
    		save as newDoc file format format Unicode text file name (desktopTestFolder & mychoice & ".txt")
    		close document 1 saving no
    	end tell
    end handler1
    
    on handler2()
    	tell application "Microsoft Word"
    		run VB macro macro name "Normal.NewMacros.Clean"
    		run VB macro macro name "Normal.Module9.bold"
    		
    		save as newDoc file format format Unicode text file name (desktopTestFolder & mychoice & ".txt")
    		close document 1 saving no
    	end tell
    end handler2
    
    Thanks,
    John
     
  2. Red Menace macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #2
    Variables defined in a handler are local to that handler unless declared otherwise. What may not be obvious is that there is an implied run handler that contains the outermost script statements, so variables defined at the beginning of your script are not seen by the other handlers unless you declare them as properties or globals.
     

Share This Page