Wednesday, November 20, 2019

#1 2016-10-23 06:56:37 am

iRounak
Member
Registered: 2011-12-04
Posts: 7

fix a string

Applescript:

set a to "zip -r foo . -i \*.c"

gives error

Expected “"” but found unknown token.


How to handle the above problem?

Offline

 

#2 2016-10-23 08:07:43 am

StevenH
Member
Registered: 2016-09-17
Posts: 29

Re: fix a string

Try \\* instead of \*

Offline

 

#3 2016-10-23 08:51:55 am

iRounak
Member
Registered: 2011-12-04
Posts: 7

Re: fix a string

I am creating json file from paragraphs of text file. The above string is one such para from the text file. Could someone fix the problem here:
https://github.com/mgax/applescript-json

Offline

 

#4 2016-10-23 04:24:11 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6036

Re: fix a string

iRounak wrote:

I am creating json file from paragraphs of text file.


Then try this (requires Yosemite or later):

Applescript:

use scripting additions
use framework "Foundation"

-- pass a string, list, record or number, and either a path to save the result to, or missing value to have it returned as text
on convertASToJSON:someASThing saveTo:posixPath
   --convert to JSON data
   set {theData, theError} to current application's NSJSONSerialization's dataWithJSONObject:someASThing options:0 |error|:(reference)
   if theData is missing value then error (theError's localizedDescription() as text) number -10000
   if posixPath is missing value then -- return string
       -- convert data to a UTF8 string
       set someString to current application's NSString's alloc()'s initWithData:theData encoding:(current application's NSUTF8StringEncoding)
       return someString as text
   else
       -- write data to file
       theData's writeToFile:posixPath atomically:true
       return result as boolean -- returns false if save failed
   end if
end convertASToJSON:saveTo:


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#5 2019-10-22 10:20:49 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3635

Re: fix a string

Hello Shane

It's an old thread but  I retrieved your handler while making some cleaning.
Trying to use it I got errors.

I tried with :

Applescript:

set theString to "just for try"
my convertASToJSON:theString saveTo:(missing value)

and got error "*** +[NSJSONSerialization dataWithJSONObject:options:error:]: Invalid top-level type in JSON write" number -10000

I made an other attempt with:

Applescript:

set theString to "just for try"
set theString to current application's NSString's stringWithString:theString
my convertASToJSON:theString saveTo:(missing value)

and got the same error.

What am I doing wrongly ?

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 22 octobre 2019  18:18:21

Offline

 

#6 2019-10-22 10:42:47 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 720

Re: fix a string

it seems, even with single string, it will be provided to handler into the list braces:

{"just for try"}

Last edited by KniazidisR (2019-10-22 10:44:17 am)


Model: MacBook Pro
macOS Mojave -- version 10.14.4
Safari -- version 12.1
Firefox -- version 70.0

Offline

 

#7 2019-10-22 12:06:33 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3635

Re: fix a string

Thanks.
I really didn't guess that it was that.

Applescript:

set theString to "just for try"
set value1 to my convertASToJSON:{theString} saveTo:(missing value)

set theList to {"other try", "31/12/1943", 3.14159}
set value2 to my convertASToJSON:theList saveTo:(missing value)

log value1 & linefeed & value2
(*["just for try"]
["other try","31\/12\/1943",3.1415899999999999]*)


value1 & linefeed & value2
(*
"[\"just for try\"]
[\"other try\",\"31\\/12\\/1943\",3.1415899999999999]"
*)

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 22 octobre 2019  20:05:54

Offline

 

#8 2019-10-22 05:05:02 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6036

Re: fix a string

Yvan,

JSON is defined as a way of serializing attribute–value pairs (records, basically) and arrays, so you must pass one or the other.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#9 2019-10-23 03:01:44 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3635

Re: fix a string

Thanks Shane. It make sense.

I was testing according to what was written in the script:

pass a string, list, record or number, and either a path to save the result to, or missing value to have it returned as text

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 23 octobre 2019  11:01:39

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)