Writing .gdoc files from a web browser - lmmx/devnotes GitHub Wiki
Google Drive now downloads Google Docs .gdoc
files as Microsoft Word .docx
. This is useless as the conversion doesn't work very well, but at present there's not an option to turn the conversion off as for uploads.
- Docs can be retained in the original format and opened in the browser, as on Chrome OS, by making a
.desktop
application and customising Nautilus (but adding new MimeTypes is inconsistent on Ubuntu) - Not possible to download Docs via Drive cli, so have to resort to browser scripting
.gdoc
files are converted to Word but the format is very simple
{"url": "URL", "resource_id": "document: ID" }
in which the URL contains the ID:
https://docs.google.com/document/d/ID/edit?usp=docslist_api
thus the files can be written knowing just the ID ($ID
):
{"url": "https://docs.google.com/document/d/$ID
/edit?usp=docslist_api", "resource_id": "document:$ID
" }
The files in a folder can be Javascript-parsed with:
icons = document.querySelectorAll('div[aria-label*="Google Docs"][class="k-u-P-m"]'); var TitleRegEx = new RegExp("Google\ Docs$","g") var thesedocs = []; for (i=0;i<icons.length;i++) { if (icons[i].parentNode.parentNode.parentNode.parentNode.parentNode.getAttribute('aria-label') == "Folder list view") { var thisdoc =['','']; thisdoc[0] = icons[i].getAttribute('aria-label').replace(TitleRegEx, "").trim(); thisdoc[1] = icons[i].getAttribute('data-id'); thesedocs.push(thisdoc.join('\t')); } } console.log(thesedocs.join('\n')); // copy(thesedocs.join('\n'));
The output can be pasted into tab-separated files for each folder, gdoc-list.tsv
.
for gdoclist in ./*/gdoc-list.tsv; do gdocdir=$(dirname "$gdoclist") while read gdocline; do gdocTitle=$(echo "$gdocline" | cut -d $'\t' -f 1) gdocID=$(echo "$gdocline" | cut -d $'\t' -f 2) echo '{"url": "https://docs.google.com/document/d/'"$gdocID"'/edit?usp=docslist_api", "resource_id": "document: "'"$gdocID"'" }' > "$gdocdir/$gdocTitle".gdoc; done <"$gdoclist" done
All files are now back where they should be.
- download whole folders as zip,
- delete the .docx converted documents
- run the above Javascript/shell scripts
-
gdoc
files now back in folders as normal and open as expected with a custom application