Re: Acquiring the Dropbox Folder Path -- was: Re: Error -10000 for one user
Re: Acquiring the Dropbox Folder Path -- was: Re: Error -10000 for one user
- Subject: Re: Acquiring the Dropbox Folder Path -- was: Re: Error -10000 for one user
- From: Nigel Garvey <email@hidden>
- Date: Wed, 27 Apr 2016 15:55:48 +0100
Stockly, Ed wrote on Wed, 27 Apr 2016 00:50:43 +0000:
>Since the name is included with the path, this script doesn't care what
>the name is, and should work even if there are more than two (which,
>apparently, is possible).
Ah. Thanks for the confirmation of this possibility. The ASObjC script I
posted on Sunday (which contains an editing error, but fortunately works
as intended) allows for more than two Dropboxes and includes the name of
the key associated with each. But not knowing how the json data might be
structured in such cases, I assumed there'd just be more entries with
different keys in the same "record", which I suppose needn't necessarily
be so. In the light of current ignorance, concentrating on the path
strings may be the best idea.
>set dropBoxJsonFile to "~/.dropbox/info.json"
>tell application "System Events" to set dropBoxJsonFile to POSIX path of
>disk item dropBoxJsonFile
>set dropBoxInfo to read dropBoxJsonFile
>set AppleScript's text item delimiters to {"{\"path\": \"", "\",
\"host\":
>"}
My comment on Saturday about not assuming the order of labelled data was
intended to point out that it may not be safe to assume paths will
always be followed by "host" keys. If we also guard against the
(admittedly unlikely) possibility that they may contain quotes or
backslashes, the only sure end markers are quotes not immediately
preceded by literal backslashes:
set jsonPath to (path to home folder as text) & ".dropbox:Info.json"
set jsonText to (read file jsonPath as «class utf8»)
set astid to AppleScript's text item delimiters
-- Break the text before each path and parse each fragment except the first.
set AppleScript's text item delimiters to "\"path\": \""
set dropboxPaths to rest of jsonText's text items
repeat with thisPath in dropboxPaths
-- Get the text of this fragment up to the character before the first quote not preceded by a backslash.
set AppleScript's text item delimiters to "\""
set i to 1
repeat while (text item i of thisPath ends with "\\")
set i to i + 1
end repeat
set edit to text 1 thru text item i of thisPath
-- Also remove one layer of any character-escaping in it.
set AppleScript's text item delimiters to "\\"
set edit to edit's text items
repeat with i from 2 to (count edit)
if (item i of edit is "") then set item i of edit to "\\"
end repeat
set AppleScript's text item delimiters to ""
-- Store what's left.
set thisPath's contents to edit as text
end repeat
set AppleScript's text item delimiters to astid
return dropboxPaths
The same thing with sed would be:
return paragraphs of (do shell script "cat ~/.dropbox/Info.json |
sed -E '# Insert linefeeds before each path.
s|\"path\": ?\"/|\\'$'\\n''/|g' |
sed -En '
/^\\// { # In each line beginning with a slash:
# Cut from the first quote not preceded by a backslash.
s|([^\\])\".+|\\1|
# Remove one layer of any escaping.
s|\\\\(.)|\\1|g
# Print what’s left.
p
}'")
NG
_______________________________________________
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