Need Help Scripting Google Drive

Hey guys, anyone have any experience with scripting Google Drive on the Mac?

I need to do the following:
Given the path to a file in a Google Drive folder on the Mac, get the:

  • Shared Link URL
  • HTML Embed Code

Any help or suggestions will be greatly appreciated.

The Google Drive app, “Backup and Sync from Google.app” has no scripting support.
But once installed, it does provide a context menu:
image

But I don’t see a way to access that given just the path of the file.

I use the Google Drive File Stream app; actually, just started using it.

In AppleScript, I need determine the root of the drive. I noticed when the app starts, it creates an alias in the home folder. When it stops, it removes the alias. Basically, i use the following code to determine the root.

on get_google_drive_root()
	try
		return do shell script "readlink ~/Google\\ Drive\\ File\\ Stream"
	on error
		-- The Google Drive File Stream app is probably not running
		return missing value
	end try
end get_google_drive_root

The root is a virtual drive (/Volumes/GoogleDrive). In there, I have two (unfortunately localized) folders, I guess the English names are this:

  • My Drive
  • Shared drives

You might not have the Shared Drives, since that’s some kind of setting. Is this helpful?

Thanks for the suggestion and example script. Unfortunately, I cannot use Google Drive File Stream since it is available only for education and corporate accounts. IAC, I did not find any features in it that looked helpful in achieving my objectives.

You might need to program against the API.
Does this stack overflow post help?

@doekman, many thanks. That was very helpful.

What I would really like to do is use JXA to run the Google Drive JavaScript API.
Don’t guess you’d have any ideas about how to do that?

Well, you could go the do shell script route, and user curl or python to do the REST calls. Python has the advantage of being well documented on the Google site. Downsides are python support might go away in the future on macOS, plus the Google docs use packages, so you might need to use a virtual environment. With curl, you don’t have these issues.

And you also could do the REST calls with the Objective-C API. Never done it with JXA or AppleScript however…

I haven’t used the Google Drive API, but I do have experience in other API’s. The thing that set me back with OAuth: with web-applications you let the web-app user authenticate against the API’s, so basically the user authenticates with Google.

With AppleScript apps this is most times not the case. So you need some kind of server-account (or re-use you personal account, but that’s not advisable).

So I think it’s doable, but you’re mostly on your own. I have no plans to do this in AppleScript. I’m moving to an web-application (python+flask) to do the heavy lifting.

@doekman, thanks again for your suggestions and advice.

I have given up using AppleScript or JXA with Google Drive API.
So I’m ready to use any language that will get the job done.

My biggest stumbling block was finding out how to ID a file just added to a Mac Google Drive folder, and then get the GD Shared URL. I can easily get the path of the file, but GD API does NOT use file paths. So it looks like I have to use some other means to ID the file with GD API.

Hi! I was also looking for a solution for this. I ended up using a workaround which calls the context menu. I got it from colossatr0n in stackoverflow . com/questions/58409124/applescript-do-a-context-menu-action. It is slower but does the job:

tell application "System Events" to tell application process "Finder"
set selectedFile to value of attribute "AXFocusedUIElement"
tell selectedFile to perform action "AXShowMenu"
delay 0.3
keystroke "Copy link"
delay 0.2
-- Sends enter
key code 36
end tell

You will have to deal with the assistive permissions issue that AppleScript lack, explained by Beejor in apple.stackexchange . com/questions/144477/can-only-run-applescript-from-automator. Basically create an AppleScript workflow to call a standalone AppleScript App in the same folder. You save an Application from Automator by going to File > Save As (maintaining the Option key), then in the saving dialog, choose to save as Application.

1 Like