-------------------------------------------------------------------------------------------
Of course for multiple Dropboxes some fiddling will be needed.
-------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
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
# Use Shane's handler to extract the path from “Personal” record of the JSON string:
set dropBoxInfoRecASObjC to my convertJSONToAS:dropBoxInfo isPath:false
set dropboxPersonalPathASObjC to |path| of personal of dropBoxInfoRec
# Use Shane's handler to extract the path from “Personal” record of the JSON file:
set dropboxInfoFile to POSIX path of ((path to home folder as text) & ".dropbox:info.json")
set dropboxPersonalPathASObjCFromFile to my convertJSONToAS:dropboxInfoFile isPath:true
set dropboxPersonalPathASObjCFromFile to |path| of personal of dropBoxInfoRec
-------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
on convertJSONToAS:jsonStringOrPath isPath:isPath
if isPath then -- read file as data
set theData to current application's NSData's dataWithContentsOfFile:jsonStringOrPath
else -- it's a string, convert to data
set aString to current application's NSString's stringWithString:jsonStringOrPath
set theData to aString's dataUsingEncoding:(current application's NSUTF8StringEncoding)
end if
-- convert to Cocoa object
set {theThing, theError} to current application's NSJSONSerialization's JSONObjectWithData:theData options:0 |error|:(reference)
if theThing is missing value then error (theError's localizedDescription() as text) number -10000
-- we don't know the class of theThing for coercion, so...
if (theThing's isKindOfClass:(current application's NSArray)) as integer = 1 then
return theThing as list
else
return item 1 of (theThing as list)
end if
end convertJSONToAS:isPath:
-------------------------------------------------------------------------------------------
1) Uses the JSON Helper.app from the App-Store to convert the JSON string into an AppleScript record.
2) Uses Shane Stanley's ASObjC handler to extract the path from “Personal” record of the JSON string.
3) Uses Shane Stanley's ASObjC handler to extract the path from “Personal” record of the JSON file.