Applescript? What's wrong with mine? urgent help needed!

Discussion in 'Mac Programming' started by billyzenme, Dec 21, 2009.

  1. billyzenme macrumors member

    Joined:
    Dec 21, 2009
    #1
    Hello Macrumor members, I'm new to the whole Applescript scene and i need a bit of urgent help with my popup window/dialogue/screen.

    What I want to do is get my user to select Alpha or Beta or Gamma or Omega or Theta and each option corresponds to a specific Microsoft Word document. Right now, the whole thing runs (or compiles) if I save it as an application. It opens the desired M$ Word document, but with a big issue.

    If i run "Beta" and close it and then later if I open "Gamma", "Beta" would appear again (as well as Gamma which will open on top). I need to know how I can prevent that past "history" thingy (opening an old document at the same time) from happening.

    Please help guys! thx in advance. :thx:

    Here's a copy of my code:
    Code:
    set receipt to choose from list {"Alpha", "Beta", "Gamma", "Omega", "Theta"} with prompt "Greek me?" OK button name "Holla" cancel button name "Doh!"
    
    if receipt is {"Alpha"} then say "Tens"
    if receipt is {"Alpha"} then set filepath to POSIX path of "Macintosh HD:Users:dee:Documents:Receipts:Tens.docx"
    try
    set command to "open " & quoted form of filepath
    do shell script command
    end try
    if receipt is {"Alpha"} then quit
    
    if receipt is {"Beta"} then say "Hundreds"
    if receipt is {"Beta"} then set filepath to POSIX path of "Macintosh HD:Users:dee:Documents:Receipts:Hundreds.docx"
    try
    set command to "open " & quoted form of filepath
    do shell script command
    end try
    if receipt is {"Beta"} then quit
    
    if receipt is {"Gamma"} then say "Thousands"
    if receipt is {"Gamma"} then set filepath to POSIX path of "Macintosh HD:Users:dee:Documents:Receipts:Thousands.docx"
    try
    set command to "open " & quoted form of filepath
    do shell script command
    end try
    if receipt is {"Gamma"} then quit
    
    if receipt is {"Omega"} then say "Millions"
    if receipt is {"Omega"} then set filepath to POSIX path of "Macintosh HD:Users:dee:Documents:Receipts:Millions.docx"
    try
    set command to "open " & quoted form of filepath
    do shell script command
    end try
    if receipt is {"Omega"} then quit
    
    if receipt is {"Theta"} then set filepath to POSIX path of "Macintosh HD:Users:dee:Documents:Receipts:Billions.docx"
    try
    set command to "open " & quoted form of filepath
    do shell script command
    end try
    if receipt is {"Theta"} then quit
    
     
  2. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #2
    Well first I would probably isolate each choice into its own "if" block. The way you have it now, it's running some code for each item up to the one you want whether it was chosen or not (your "try" blocks are not inside any "if" blocks). I'm not sure that's your whole problem, but try that first.

    Something like this:

    Code:
    if receipt is {"Alpha"} then
    	say "Tens"
    	set filepath to POSIX path of "Macintosh HD:Users:dee:Documents:Receipts:Tens.docx"
    	try
    		set command to "open " & quoted form of filepath
    		do shell script command
    	end try
    end if
    
    if receipt is {"Beta"} then
    	...
    end if
    
     
  3. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #3
    HiRez picked out a couple of things. I have a few more:

    1. Each item in your list -- Alpha, Beta, etc. -- should be bracketed in an if/else if/then block. If you allowed multiple selections, you might want to do it your way, but you'd have to parse through each item in receipt, because choose from list could return several items, not just one. Something like this:

    Code:
    set receipt to choose from list {"Alpha", "Beta", "Gamma", "Omega", "Theta"} with prompt "Greek me?" OK button name "Holla" cancel button name "Doh!"
    
    if receipt is {"Alpha"} then
    	say "Tens"
    	set filepath to POSIX path of "Macintosh HD:Users:dee:Documents:Receipts:Tens.docx"
    	try
    		set command to "open " & quoted form of filepath
    		do shell script command
    	end try
    else if receipt is {"Beta"} then
    	say "Hundreds"
    	set filepath to POSIX path of "Macintosh HD:Users:dee:Documents:Receipts:Hundreds.docx"
    	try
    		set command to "open " & quoted form of filepath
    		do shell script command
    	end try
    else if receipt is {"Gamma"} then
    	say "Thousands"
    	set filepath to POSIX path of "Macintosh HD:Users:dee:Documents:Receipts:Thousands.docx"
    	try
    		set command to "open " & quoted form of filepath
    		do shell script command
    	end try
    else if receipt is {"Omega"} then
    	say "Millions"
    	set filepath to POSIX path of "Macintosh HD:Users:dee:Documents:Receipts:Millions.docx"
    	try
    		set command to "open " & quoted form of filepath
    		do shell script command
    	end try
    else -- "if receipt is {"Theta"} then" is unnecessary because this will be a catchall
    	set filepath to POSIX path of "Macintosh HD:Users:dee:Documents:Receipts:Billions.docx"
    	try
    		set command to "open " & quoted form of filepath
    		do shell script command
    	end try
    end if
    2. Why are you using a try block without capturing any errors? This might help explain your "history" issue.

    3. Why use "do script" to open the file? This should do fine:

    Code:
    tell application "Microsoft Word" to open "[I] -- long file name -- [/I]"
    
    That should also eliminate the need to convert an HFS filename to POSIX.

    None of this gets directly at your "history" problem, and in my case, I'm not picking up exactly what the issue is. Sorry if I sound dense.

    Do you anticipate the user run your script to choose a file, then run your script a second time? What happens to the original file? This sounds like a Microsoft Word issue, not a script issue.

    You might need a line at the top of the script to say:

    Code:
    tell application "Microsoft Word" to close window 1 saving yes
    I don't have Word on my computer, so the syntax may be different. Hope this helps.

    mt
     

Share This Page