AppleScript please help: probably small problem within if statement

Discussion in 'Mac Programming' started by pep, Mar 18, 2010.

  1. pep macrumors newbie

    Joined:
    Mar 17, 2010
    #1
    Hi guys,

    I have a perfectly working applecsript for TextWrangler (below: all the stuff between the first else and last end if) and only wanted to make it activate textwrangler if i work in another application; if i'm in textwrangler i want it to run the rest of the script. however, when i try to run this the applescript editor gives me an error complaining that "Expected “end” but found “on”." referring to the "on ComputeTempFileName(originalFile)" line.

    I'm new to this... and none of what i tried worked.

    Would you have any idea how to fix this? thanks a lot. Josef


    if name of current application is not "textwrangler"
    tell application "textwrangler" to activate
    else

    tell application "TextWrangler"
    set thecontents to the selection in window 1 as string
    set theOriginalFile to file of document 1
    if thecontents is "" then
    tell text window 1
    select line (startLine of selection)
    end tell
    end if
    set thecontents to the selection in window 1 as string
    set theOriginalFile to file of document 1
    end tell
    set thecontents to thecontents & return & "erase temp.do" & return & "exit"
    set tempFile to my ComputeTempFileName(theOriginalFile)
    my write_to_file(thecontents, tempFile, false)
    tell application "Finder" to set filename to tempFile as alias
    tell application "StataSE"
    activate
    get name
    end tell
    if name of application "StataSE" = "StataSE" then

    tell application "System Events"
    tell process "StataSE"
    set frontmost to true
    tell window 1
    get name
    end tell
    if name of window 1 = "data editor" then

    tell window "data editor"
    keystroke "w" using command down
    end tell
    end if
    end tell
    end tell
    tell application "StataSE"
    activate
    open filename
    end tell
    end if
    on ComputeTempFileName(originalFile)
    tell application "Finder"
    set c to container of originalFile
    set t to (c as string) & "temp.do"
    end tell
    return t
    end ComputeTempFileName

    on write_to_file(this_data, target_file, append_data)
    tell application "Finder"
    try
    set the target_file to the target_file as text
    set the open_target_file to ¬
    open for access file target_file with write permission
    if append_data is false then ¬
    set eof of the open_target_file to 0
    write this_data to the open_target_file starting at eof
    close access the open_target_file
    return true
    on error
    try
    close access file target_file
    end try
    return false
    end try
    end tell
    end write_to_file
    end if
     
  2. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #2
    Problem 1 is functions like ComputeTempFileName and write_to_file need to be top level, that is, they shouldn't be indented.

    You should rewrite your script to work something like:

    Code:
    if name of current application is not "textwrangler" then
    	tell application "TextWrangler" to activate
    else
    	domybidding
    end if
    And put the guts of your script into "on domybidding".

    Also, learn how to use the [ CODE ] [ /CODE ] tags. It makes reading these things a lot easier.

    Also, you might want to rework that try block in in the write_to_file function. You might get an error but you won't know how to fix it.

    mt
     
  3. -aggie- macrumors P6

    -aggie-

    Joined:
    Jun 19, 2009
    Location:
    Where bunnies are welcome.
    #3
    First post this in the programming forums.

    Also use
    PHP:
     tags (or code tags).

    I tried to compile your codebut you've got so many if statements, try'and tell's that it is impossible, since you didn't indent anything.  From what I can tellit looks like you have messed up your if statements.  You need to format this code correctly and use indents to visually show how your if's and end if's go togetheryour try's and end try's go togetheretc.

    In a phrasethis code is a mess.  :)
     
  4. pep thread starter macrumors newbie

    Joined:
    Mar 17, 2010
    #4
    Thanks a lot MT, I'm new to this, but i eventually figured out:

    Code:
    tell application "System Events"
    	set appname to name of the first process whose frontmost is true
    end tell
    if appname is "TextWrangler" then
    	domybidding()
    else
    	tell application "TextWrangler" to activate
    end if
    on domybidding()
    
     

Share This Page