• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Acquiring the Dropbox Folder Path → was: Re: Error -10000 for one user
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Acquiring the Dropbox Folder Path → was: Re: Error -10000 for one user


  • Subject: Acquiring the Dropbox Folder Path → was: Re: Error -10000 for one user
  • From: Christopher Stone <email@hidden>
  • Date: Thu, 21 Apr 2016 12:07:26 -0500

On Apr 21, 2016, at 04:09, Yvan KOENIG <email@hidden> wrote:
The target folder didn’t exist. It turns out the user had a personal Dropbox folder set up on her machine in addition to the company Dropbox folder.
______________________________________________________________________

Hey Folks,

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.

-------------------------------------------------------------------------------------------

Shell:

cat ~/.dropbox/info.json | awk '{print $3}' | sed 's/[",]//g'

-------------------------------------------------------------------------------------------

JXA:

(function () {
  return JSON.parse(
    $.NSString.stringWithContentsOfFile(
      $("~/.dropbox/info.json").stringByExpandingTildeInPath
    ).js
  ).personal.path;
})();

Of course for multiple Dropboxes some fiddling will be needed.

-------------------------------------------------------------------------------------------

Now let's see what we can do with AppleScript:

-------------------------------------------------------------------------------------------

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

-------------------------------------------------------------------------------------------
--» HANDLERS ( Shane Stanley → http://macscripter.net/viewtopic.php?id=42517 )
-------------------------------------------------------------------------------------------
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:
-------------------------------------------------------------------------------------------

There are 3 methods here:

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.

I only have my personal Dropbox to test with, so I'd be interested to see what the JSON looks like for a multi-Dropbox setup.

--
Best Regards,
Chris

 _______________________________________________
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

  • Follow-Ups:
    • Re: Acquiring the Dropbox Folder Path → was: Re: Error -10000 for one user
      • From: RJay Hansen <email@hidden>
  • Prev by Date: Re: Error -10000 for one user
  • Next by Date: Restore FastScripts Keyboard Shortcuts from Backup
  • Previous by thread: Re: OCR a PDF
  • Next by thread: Re: Acquiring the Dropbox Folder Path → was: Re: Error -10000 for one user
  • Index(es):
    • Date
    • Thread