Re: Acquiring the Dropbox Folder Path $)A!z was: Re: Error -10000 for one user
Re: Acquiring the Dropbox Folder Path $)A!z was: Re: Error -10000 for one user
- Subject: Re: Acquiring the Dropbox Folder Path $)A!z was: Re: Error -10000 for one user
- From: Nigel Garvey <email@hidden>
- Date: Fri, 22 Apr 2016 15:03:23 +0100
Christopher Stone wrote on Thu, 21 Apr 2016 12:07:26 -0500:
>We had a long conversation about the Dropbox path on the Keyboard
Maestro
>list, and our final conclusion was that it's a best practice to extract
its
>location from its own files and completely eliminate guessing.
[snip]
>Shell:
>
>cat ~/.dropbox/info.json | awk '{print $3}' | sed 's/[",]//g'
[snip]
>Of course for multiple Dropboxes some fiddling will be needed.
[snip]
>use framework "Foundation"
>use scripting additions
>
>set dropBoxJsonFile to "~/.dropbox/info.json"
>
># Read the JSON file:
>tell application "System Events" to set dropBoxJsonFile to POSIX path of
>disk item dropBoxJsonFile
>set dropBoxInfo to read dropBoxJsonFile as «class utf8»
>
># Convert to an AppleScript Record with the “JSON Helper” app from the
>App-Store:
>tell application "JSON Helper" to set dropBoxInfoRec to read JSON from
>dropBoxInfo
># Extract the path from the “Personal” Dropbox record:
>set dropboxPersonalPath to |path| of personal of dropBoxInfoRec
Hi Chris,
This seems to be a fairly reliable way to derive the record:
set dropboxInfoRec to (run script (do shell script "cat ~/.dropbox/info.json | sed -E 's/\"([[:alnum:]_]+)\":/|\\1|:/g'"))
The same thing's possible using ASObjC instead of the shell script, but
with a few more lines of code. ;)
Alternatively, if, as it seems, Dropbox paths are the only things in the
text beginning with slashes and followed by unescaped quotes, they might
be parsed out directly like this:
use framework "Foundation"
set jsonPath to "~/.dropbox/info.json"
tell current application's class "NSString"
set jsonFullPath to (its stringWithString:jsonPath)'s stringByExpandingTildeInPath()
set jsonText to its stringWithContentsOfFile:jsonFullPath encoding:(current application's NSUTF8StringEncoding) |error|:(missing value)
end tell
set dropboxPaths to jsonText's stringByReplacingOccurrencesOfString:"(^|(?<!\\\\)\")[^/]+" withString:linefeed options:(current application's NSRegularExpressionSearch) range:{0, jsonText's |length|()}
set dropboxPaths to paragraphs 2 thru -2 of (dropboxPaths as text)
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