How do you let the user change the code?

Discussion in 'Mac Programming' started by Intelligent, May 20, 2014.

  1. Intelligent, May 20, 2014
    Last edited: May 20, 2014

    Intelligent macrumors 6502a

    Intelligent

    Joined:
    Aug 7, 2013
    #1
    Is this very complicated? I'm using applescript.

    Is there a way to let the user make changes to the code like this, for example:
    Code:
    on mybuttonhandler278_(sender)
         do shell script "sudo rm -rf /path/to/file/
    end
    
    But let the user choose what the /path/to/file should be?

    or like this, another example:
    Code:
    on idle
    	tell application "System Events"
    		tell current location of network preferences
    			set myConnection to the service "TheName of yourvpn"
    			if myConnection is not null then
    				if current configuration of myConnection is not connected then
    					connect myConnection
    				end if
    			end if
    		end tell
    		return 120
    	end tell
    end idle
    
    And let the user type somewhere to change the yourvpn name?
     
  2. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #2
    If I understand what you're asking correctly, then you could either ask the user to type the path manually, for example:

    Code:
    set thePath to text returned of (display dialog "Please enter the path..." default answer "/path/to/file/")
    or you could ask them to browse for it:

    Code:
    set thePath to POSIX path of (choose folder with prompt "Please choose a folder to delete")
    Personally I'd probably go for the latter because it avoids typos.

    Hope that helps
    Rob
     
  3. Intelligent thread starter macrumors 6502a

    Intelligent

    Joined:
    Aug 7, 2013
    #3
    Hey! Thanks i really hope it works, didn't know it would be that simple :D. Thank you really much!
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    An AppleScript that prompts the user for a path then erases it for really real seems like a bad idea.
     
  5. jeremysteele macrumors 6502

    Joined:
    Jul 13, 2011
    #5
    I doubt that's their actual script. Looks like just an example.
     
  6. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #6
    I was going to say. Especially as the example uses sudo as well. Sounds like it would be easy to delete important / critical system files by accident.
     
  7. Intelligent thread starter macrumors 6502a

    Intelligent

    Joined:
    Aug 7, 2013
    #7
    (Quote from original post)

    Is this very complicated? I'm using applescript.

    Is there a way to let the user make changes to the code like this, for example:
     
  8. JustMartin macrumors 6502a

    Joined:
    Feb 28, 2012
    Location:
    UK
    #8
    Many, many years ago, I was involved with support and one of the products had a setting that allowed you to specify your own print command so that reports could go to designated printers or through filters before printing. I supposed it was only a matter of time before we got the call from a system admin who had set it to "rm -rf /" in order to 'see what happened.' A bad idea that might have been mitigated by a backup before trying it, but that was something she didn't do.
     
  9. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #9
    I don't see how you could possibly be expected to protect them from their own idiocy like that...
     
  10. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #10
    One way is to not allow arbitrary shell commands being passed in as arguments, or not blindy execute user supplied strings. It's a bad idea.
     
  11. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #11
    Yeah, but it seems to me that the idea was to allow arbitrary commands to be entered.
     
  12. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #12
    I don't know of the specific situation, but what was mentioned was print commands. So the input should be checked for valid commands, executing any string allows for all sorts of things, especially due to escape characters and other similar shell features. SQL injection comes to mind as a similar class of problem.
     
  13. Intelligent thread starter macrumors 6502a

    Intelligent

    Joined:
    Aug 7, 2013
    #13
    Thanks for the posix path thing but how about the vpn connection?
     
  14. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #14
    The name of the VPN connection is a variable.

    Apply the same principle. Specifically, ask the user for the value of the variable, then store it for ongoing use. That is, get the variable's value, then store it in a persistent way.

    You should look at AppleScript's properties for how to store things for later use (persistence).

    You can also use the 'defaults' command in a 'do shell script', but look at AppleScript properties first.
     
  15. Intelligent thread starter macrumors 6502a

    Intelligent

    Joined:
    Aug 7, 2013
    #15
    Code:
    set myConnection to the text returned of (display dialog "Enter the name of your VPN" default answer "VPN")
    
    this is what i did but it doesn't recognize the text as a "service". I dont know how to make it into a service, since this doesn't work either

    Code:
    set myConnection to the service of the text returned of (display dialog "Enter the name of your VPN" default answer "VPN")
    
     
  16. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #16
    Step 1:
    Does the code you posted actually work?

    I'm referring to the code in your first post, regarding setting a service. That is, if you use a string that is the exact name of your actual VPN service, does the posted code actually work to set the VPN?

    If it doesn't work, then you need to fix it so it does. If you don't know how to fix it, then you need to learn how to set services using AppleScript.


    Step 2:
    What are the actual services a user has on their computer? How would you discover them?

    Try this code:
    Code:
    tell application "System Events"
    	tell current location of network preferences
    		get  every service
    	end tell
    end tell
    
    The result should be a list containing services.

    Each service has some properties. You should know enough AppleScript to be able to look at the scripting dictionary of System Events and related scriptable targets to figure out how to get those properties.

    For example, one property of a service is its name. Replace this line:
    Code:
    get every service
    
    with this line:
    Code:
    get name of every service
    
    and rerun the script. What is the result? How does it differ?

    Now, knowing what you've just discovered, and knowing that you can look at the scripting dictionary of StandardAdditions.osax to learn about presenting dialogs, lists, etc., come up with a plan to do experiments, find a way of interacting with the user, and present a list of service names. Then using the chosen name, use AppleScript to get the service whose name is the one chosen, and set that as the service.

    If you don't know enough AppleScript to do that, then you need to learn it. There are plenty of tutorials on the subject, and probably even some with examples of setting a service by name.

    If you already done an AppleScript tutorial or book, exactly which ones? Be specific.
     
  17. Intelligent thread starter macrumors 6502a

    Intelligent

    Joined:
    Aug 7, 2013
    #17
    The first script do work when replacing it with the name of my own vpn.
     

Share This Page