Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Intelligent

macrumors 6502a
Original poster
Aug 7, 2013
922
2
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?
 
Last edited:
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
 
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

Hey! Thanks i really hope it works, didn't know it would be that simple :D. Thank you really much!
 
An AppleScript that prompts the user for a path then erases it for really real seems like a bad idea.

(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:
 
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.

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.
 
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.

I don't see how you could possibly be expected to protect them from their own idiocy like that...
 
I don't see how you could possibly be expected to protect them from their own idiocy like that...

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.
 
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.

Yeah, but it seems to me that the idea was to allow arbitrary commands to be entered.
 
Yeah, but it seems to me that the idea was to allow arbitrary commands to be entered.

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.
 
Thanks for the posix path thing but how about the vpn connection?

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.
 
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.

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")
 
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.
 
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.

The first script do work when replacing it with the name of my own vpn.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.