Re: Handler does not accept space in textstring
Re: Handler does not accept space in textstring
- Subject: Re: Handler does not accept space in textstring
- From: Christopher Nebel <email@hidden>
- Date: Wed, 05 Mar 2014 17:32:40 -0800
On Mar 5, 2014, at 3:56 PM, Iurista GmbH <email@hidden> wrote:
> I would like to write to a plist file via an applescript file functioning as a handler.
>
> The calling script is like this
> set {act, gid, val} to {"s", "app__MyAppsPfad", "Applications:other Software:"}
> set UH to alias ("Shared:Scripting:Applescript Corner:Universelle Handlers:ash| GlobalServer.scpt")
> run script (UH) with parameters {act, gid, val}
>
> the handler script "ash| GlobalServer.scpt" has the following code:
> on run {act, gid, val}
> -- some lines of code not interesting here
> if act = "s" then -- Schreiben
> set x to ("defaults write com.durrer.Globals ) & gid & " " & val)
> # return x -- this one is here only for testing purposes
> do shell script x
> end if
>
> The probem is:
> When the variable val contains a space (" ", like above in "Applications:other Software:"), the script calling the handler returns an error.
>
> What do I wrong in scripting?
Quoting from <https://developer.apple.com/library/mac/technotes/tn2065>:
> Q: My command doesn’t work right when a parameter has spaces or certain punctuation — parentheses, $, *, etc.
>
> A: Because the shell separates parameters with spaces, and some punctuation marks have special meanings, you must take special steps to make the shell treat your string as one parameter with literal spaces, parentheses, etc. This is called "quoting," and there are several ways to do it, but the simplest and most effective is to use the quoted form property of strings.
>
> For example, consider this (buggy) handler, which takes a string and appends it to a file named "stuff" in your home directory:
>
> to append_message(s)
> do shell script "echo " & s & " >> ~/stuff”
> end append_message
>
> It works fine for most strings, but if we call it with a string like "$100", the string that ends up in the file is "00", because the shell thinks that "$1" is a variable whose value is an empty string. (Variables in sh begin with a dollar sign.) To fix the script, change it like this:
>
> do shell script "echo " & quoted form of s & " >> ~/stuff”
>
> The quoted form property gives the string in a form that is safe from further interpretation by the shell, no matter what its contents are. For more details on quoting, see the sh man page under "Quoting."
People will sometimes advise replacing space characters in the string with “\ “, or using “quote & act & quote”, etc. Don’t. If you try to implement quoting yourself, you *will* get it wrong. Use “quoted form of” instead.
—Chris N.
AppleScript Engineering
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden