Sunday, November 29, 2020

#1 2020-10-28 11:56:29 pm

scrutinizer82
Member
Registered: 2015-08-04
Posts: 54

osascript ignores escaped quotes

I need to pass osascript this string as an argument after -e

'tell application "iTunes" to set current EQ preset to EQ preset named "Bestial devastation +10"'

 

The equalizer preset used for the demo purpose is mine. I store the preset's name in a shell variable Preset. I'm able to assemble this string in the command line. The solution I came up with is as follows:

Preset="Bestial devastation +10" ; echo "'tell application \"iTunes\" to set current EQ preset to  \"$Preset\"'" | xargs -0



However, it chokes as soon as I add osascript as in

Preset="Bestial devastation +10" ; echo "'tell application \"iTunes\" to set current EQ preset to  \"$Preset\"'" | xargs -0 -I % osascript -e %



I ran this shell script as do shell script in ScriptEditor, and no matter how I bend it it compiles as

'tell application iTunes to set current EQ preset to EQ preset named Bestial devastation +10'



which, of course, fires up the endless torrent of complains that "this token can't go after that identifier". I tried to turn on -o and -t switches to xargs to start it in interactive mode and "echo" the command to stderr before it being executed (all according to the xargs manual) but stumbled into the same wall: incorrect syntax related to an out-of-place identifier.

What do I do wrong? How to pipe to osascript a string with single and double quotes preserved in the command line as the result of the previous command?
(To test, put the name of any preset between the quotes in the variable Preset).

Model: MacBook Pro
AppleScript: 2.3.2
Browser: Safari 537.86.7
Operating System: macOS 10.9

Last edited by scrutinizer82 (2020-10-28 11:59:28 pm)


Scripting in Mac OS X 10.7.5, OS X 10.9.5, macOS 10.14.6

Offline

 

#2 2020-10-29 02:28:16 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5312

Re: osascript ignores escaped quotes

Hi.

It can get pretty hairy when you're dealing with quotes within quotes within quotes.  smile

I haven't tested this with iTunes, but an equivalent test script for the Finder works OK in Mojave:

Applescript:

do shell script "Preset=\"Bestial devastation +10\" ; echo 'tell application \"iTunes\" to set current EQ preset to \"'$Preset'\"' | xargs -0 -I % osascript -e %"


NG

Offline

 

#3 2020-10-29 02:47:54 am

scrutinizer82
Member
Registered: 2015-08-04
Posts: 54

Re: osascript ignores escaped quotes

Hi,
Thank you, but I need to run in the command line, and there your code errors out to

-bash: Preset="Bestial devastation +10" ; echo 'tell application "iTunes" to set current EQ preset to "''"' | xargs -0 -I % osascript -e %: command not found



As soon as single quoted the variable expansion falters.




Model: MacBook Pro
AppleScript: 2.3.2
Browser: Safari 537.86.7
Operating System: macOS 10.9

Last edited by scrutinizer82 (2020-10-29 02:49:48 am)


Scripting in Mac OS X 10.7.5, OS X 10.9.5, macOS 10.14.6

Offline

 

#4 2020-10-29 03:41:23 am

scrutinizer82
Member
Registered: 2015-08-04
Posts: 54

Re: osascript ignores escaped quotes

Solved.

For some reason this version worked

preset="Bestial devastation +10"; echo "'tell application \"iTunes\" to set current EQ preset to EQ preset named \"$preset\"'" | xargs -I % osascript -e %



A strange thing is that although osascript requires single-quoting arguments to its multiline mode option *-e* when supplied as standard input, the line above is comfortable with unquoted argument which is a revelation for me.

Model: MacBook Pro
AppleScript: 2.3.2
Browser: Safari 537.86.7
Operating System: macOS 10.9


Scripting in Mac OS X 10.7.5, OS X 10.9.5, macOS 10.14.6

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)