Tuesday 12 May 2020

Create a Shortcut in Google Drive

The following Google Apps Script is designed to create a Shortcut to an existing Google Drive folder, within another folder.
Screenshot of Shortcut file in Google Drive
Screenshot of Shortcut file in Google Drive

Enable Drive API
In Script Editor go to Resources > Advanced Google services ... > scroll down and turn on Drive API > click OK.

The Code
Create a new file in Google Drive that will become the Shortcut:
var shortcut = Drive.newFile();
Provide the new file with a title and icon type:
shortcut.title = 'My Shortcut';
shortcut.mimeType = 'application/vnd.google-apps.shortcut';
Now add the ID of the parent Google folder where you want the Shortcut to be created. This is required as a list within an array so it looks more daunting than it needs to be:
shortcut.parents = [
    {
      "id": "ENTER ID HERE"
    }
];
Next we create some further details about the Shortcut - such as the ID of the folder we want it to point to:
var shortcutDetails = Drive.newFileShortcutDetails();
shortcutDetails.targetId = "ENTER ID HERE";
shortcut.shortcutDetails = shortcutDetails;
Finally we insert the Shortcut into Google Drive:
Drive.Files.insert(shortcut);
And that is it.

Create a Shortcut in Google Drive.gs

2 comments:

  1. Hi Phil, id there any way that I can bulking create shortcuts of files and add them to the corresponding folders?

    Cheers
    Peter

    ReplyDelete
    Replies
    1. Hi Peter

      Since this blog post I have not done anything further with Shortcuts. From a quick reminder of the code I have written, I don't see why it wouldn't be possible to have a list of files (to create shortcuts to) and folders (for where they should be stored) in something like a Google Sheet that you can then iterate through, yes.

      Kind regards
      Phil

      Delete