0

I'm trying to make a simple program that will give a user a GUI for running the Ditto terminal command but I have run into a snag. I allow the user to select their own paths and then the variables for those paths go into a shell script that will run ditto with admin privileges.

The code I'm posting is mostly irrelevant but it will be necessary for context:

on startDitto_(sender)
    try
        set dialogResult to display dialog ¬
        "Are you ready to start copying files?" buttons {"Cancel", "Yes"} ¬
        default button "Yes" cancel button ¬
        "Cancel" giving up after 15
        on error number -128
        set userCanceled to true
    end try
    try
    if userCanceled then
        display dialog "User Cancelled"
    else if gave up of dialogResult then
        display dialog "Timed out"
    else if button returned of dialogResult is "Yes" then
        do shell script "ditto -V " & sourceFolder & " " & destFolder with administrator privileges
    end if
    end try

end

The issue lies here:

     else if button returned of dialogResult is "Yes" then
        do shell script "ditto -V " & sourceFolder & " " & destFolder with administrator privileges
     end if

Everything works great up until you hit "Yes." It doesn't continue, doesn't ask for a password, just sits. I have read a bit on embedding a script but that doesn't necessarily work since I'm using variables in the script (unless the script can use those variables but when I embedded a script.sh it didn't seem to work) and then I was lead to NSTask but I'm not well versed in Objective-C or Cocoa yet.

2 Answers 2

1

If you really want to catch all three responses, I think you'd have to look at doing something like this...

try
    set userResponse to button returned of (display dialog "Are you ready to copy?" buttons {"No", "Yes"} default button "Yes" giving up after 15)

    if userResponse = "Yes" then
        do shell script "ditto -V " & sourceFolder & " " & destFolder with administrator privileges
    else if userResponse = "No" then
        -- CANCEL
        display dialog "Canceled"
    else
        -- TIMED OUT
        display dialog "Timed out"
    end if
end try

If you really don't care about capturing the "give up" event, you could try it this way...

try
     set userResponse to button returned of (display dialog "Are you ready to copy?" buttons {"Cancel", "Yes"} default button "Yes" giving up after 2)

     if userResponse = "Yes" then
         do shell script "ditto -V " & sourceFolder & " " & destFolder with administrator privileges
     end if

on error err
     display dialog err buttons {"OK"} default button 1
end try
Sign up to request clarification or add additional context in comments.

4 Comments

This works great, thank you! I do have an additional inquiry though. I ended up moving my "Yes" if to be the first if and not an else if and for whatever reason it worked. Any reasoning why it would work if it's the first in the stack?
I'm not totally sure I understand your follow up question.
I don't quite understand it either after re-reading! What I meant was I moved, in my original block of code, the else if button returned of dialogResult is "Yes" then to being the first argument (correct term?) of the if-then and it began to run my shell script. So my question would be why does the order (or does it) of the arguments in the if - then statement matter? For example: in your code you chose to do if userResponse = "Yes" then else if userResponse = "No" then would it matter if it was changed to if userResponse = "No" then else if userResponse = "Yes" then ?
No, the order of the "Yes" and "No" in the if then statements won't matter in this case. It is however important that the third "else" option stays as the last option because it's a "catch all" option. Anything that is not a "yes" or "no" answer will end up there. @ElRojito
0

So to answer my own question (if anyone needs this in the future) I ended up using this:

  try
     display dialog "Are you ready to copy?" buttons {"Cancel", "Yes"} default button "Yes" cancel button "Cancel" giving up after 15
       set userResponse to (button returned of the result)
         if userResponse is "Yes" then
           do shell script "ditto -V " & sourceFolder & " " & destFolder with administrator privileges
         else if button returned is "Cancel" then
           return 1
         end if
  end try

Honestly, this isn't the way I want to do it, but it will work for the time being. When I get the previous way to work (with the additional cancel options) I'll post that as well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.