Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Importing desired text question.

I'm new to applescript and I was wondering if anyone had tips on how to fix this problem with this script I was working on.

If im trying to write a command into terminal it looks something like this.

________________________________________________________________________________ ____________________________________________________


activate application "Terminal"

tell "Terminal"

do script "sudo defaults write /Library/Preferences/com.apple.loginwindow LoginwindowText 'Text inserted here'"

end tell


________________________________________________________________________________ ____________________________________________________


But what I need to know how to do is something along the lines of this


________________________________________________________________________________ ____________________________________________________


set desiredtextQ to display dialog "Please enter your desired text." default answer "Text Here" buttons {"Continue", "Cancel"}

set desiredtextA to text returned of desiredtextQ

activate application "Terminal"

tell "Terminal"

do script "sudo defaults write /Library/Preferences/com.apple.loginwindow LoginwindowText 'desiredtextA'"

end tell

________________________________________________________________________________ ____________________________________________________


So basicly all I'm trying to do is take the text returned of a question and import it into a scripted terminal command.

I would be thankful if anyone had any help to share with me.

MacBook Pro (15-inch Late 2008), Mac OS X (10.7.4), AppleScript

Posted on Oct 5, 2012 9:44 AM

Reply
Question marked as Best reply

Posted on Oct 5, 2012 10:38 AM

First off, avoid scripting Terminal.app. For most purposes, including this one, there is no need and you are far, far better off using AppleScript's do shell script, which can execute a shell command without needing to invoke Terminal.app.


Therefore, I'd rewrite your first script as:


do shell script "defaults write /Library/Preferences/com.apple.loginwindow LoginwindowText 'Text inserted here'" with administrator privileges


Note that I've also nixed the 'sudo' component in favor of do shell script's '... with administrator privileges'.


As for the second part of your question, integrating a variable, that's just a matter of concatenating the strings using the & character:


set desiredtextQ to display dialog "Please enter your desired text." default answer "Text Here" buttons {"Cancel", "Continue"} default button 2

set desiredtextA to text returned of desiredtextQ


do shell script "echo defaults write /Library/Preferences/com.apple.loginwindow LoginwindowText " & desiredtextA with administrator privileges


Note that if the text from the dialog includes (or might include) spaces or other shell-unsafe characters you might need to quote the text, like:


do shell script "echo defaults write /Library/Preferences/com.apple.loginwindow LoginwindowText " & quoted form of desiredtextA with administrator privileges

1 reply
Question marked as Best reply

Oct 5, 2012 10:38 AM in response to yellowb0x

First off, avoid scripting Terminal.app. For most purposes, including this one, there is no need and you are far, far better off using AppleScript's do shell script, which can execute a shell command without needing to invoke Terminal.app.


Therefore, I'd rewrite your first script as:


do shell script "defaults write /Library/Preferences/com.apple.loginwindow LoginwindowText 'Text inserted here'" with administrator privileges


Note that I've also nixed the 'sudo' component in favor of do shell script's '... with administrator privileges'.


As for the second part of your question, integrating a variable, that's just a matter of concatenating the strings using the & character:


set desiredtextQ to display dialog "Please enter your desired text." default answer "Text Here" buttons {"Cancel", "Continue"} default button 2

set desiredtextA to text returned of desiredtextQ


do shell script "echo defaults write /Library/Preferences/com.apple.loginwindow LoginwindowText " & desiredtextA with administrator privileges


Note that if the text from the dialog includes (or might include) spaces or other shell-unsafe characters you might need to quote the text, like:


do shell script "echo defaults write /Library/Preferences/com.apple.loginwindow LoginwindowText " & quoted form of desiredtextA with administrator privileges

Importing desired text question.

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.