Skip to content

Commit

Permalink
revised getPDFReader function for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Joscha Legewie committed Feb 7, 2014
1 parent 66ea5ea commit 1be759e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 21 deletions.
27 changes: 6 additions & 21 deletions chrome/content/zotfile/openPDF-protocol-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,11 @@ var OpenPDFExtension = new function(){
}
}
if(Zotero.isWin) {
// get path to Adobe Reader
var acrobat = zz.prefs.getCharPref('pdfExtraction.openPdfWin');
// get path from registry if not set
// http://stackoverflow.com/questions/11934159/how-extension-can-read-the-registry
// 'C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe'
if (acrobat==='') {
var wrk = Components.classes["@mozilla.org/windows-registry-key;1"]
.createInstance(Components.interfaces.nsIWindowsRegKey);
wrk.open(wrk.ROOT_KEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths",
wrk.ACCESS_READ);
if(wrk.hasChild('AcroRd32.exe')) {
subkey = wrk.openChild('AcroRd32.exe', wrk.ACCESS_READ);
acrobat = subkey.readStringValue('Path') + 'AcroRd32.exe';
}
wrk.close();
}
// return if invalid path
if (!zz.fileExists(acrobat)) {
zz.infoWindow(zz.ZFgetString('general.error'), 'Unable to find path for Adobe Reader. Please install or set path in hidden preferences (see zotfile documentation).');
// get path to PDF Reader
var pdf_reader = zz.prefs.getCharPref('pdfExtraction.openPdfWin');
pdf_reader = pdf_reader==='' ? zz.getPDFReader() : pdf_reader;
if (!zz.fileExists(pdf_reader)) {
zz.infoWindow(zz.ZFgetString('general.error'), 'Unable to find path for PDF Reader. Please set path manually in hidden preferences (see zotfile documentation).');
return;
}
// open pdf on page
Expand All @@ -81,7 +66,7 @@ var OpenPDFExtension = new function(){
else
args = ['/A', '"' + path + '"'];
// run process
zz.runProcess(acrobat, args, false);
zz.runProcess(pdf_reader, args, false);
}
if(Zotero.isLinux) {
var cmd = zz.prefs.getCharPref('pdfExtraction.openPdfLinux');
Expand Down
48 changes: 48 additions & 0 deletions chrome/content/zotfile/zotfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1619,6 +1619,9 @@ Zotero.ZotFile = {
// argument: path as string (with optional filename), zotero att, or file obj
fileExists: function (arg, filename) {
var file;
// when undefined
if (arg===undefined)
return(false);
// when string is passed
if(typeof(arg)=='string') {
if(filename!=null) arg=this.completePath(arg,filename);
Expand Down Expand Up @@ -2712,6 +2715,51 @@ Zotero.ZotFile = {
this.handleErrors();
},

getPDFReader: function() {
var wrk = Components.classes["@mozilla.org/windows-registry-key;1"]
.createInstance(Components.interfaces.nsIWindowsRegKey);

//get handler for PDFs
var success = false;
var tryKeys = ['.pdf', '.PDF']
for(var i=0; !success && i<tryKeys.length; i++) {
try {
wrk.open(wrk.ROOT_KEY_CLASSES_ROOT,
tryKeys[i],
wrk.ACCESS_READ);
success = true;
} catch(e) {}
}

if(!success) return;

var progId = wrk.readStringValue('');
//get version specific handler, if it exists
try {
wrk.open(wrk.ROOT_KEY_CLASSES_ROOT,
progId + '\CurVer',

This comment has been minimized.

Copy link
@aurimasv

aurimasv Feb 7, 2014

Contributor

Sorry I didn't catch this earlier, but this should be progId + '\\CurVer' (i.e. double backslash)

wrk.ACCESS_READ);
progId = wrk.readStringValue('') || progId;
} catch(e) {}

//get command
success = false;
tryKeys = [progId + '\\shell\\Read\\command', progId + '\\Shell\\Read\\command', progId + '\\shell\\Open\\command', progId + '\\Shell\\Read\\command'];
for(var i=0; !success && i<tryKeys.length; i++) {
try {
wrk.open(wrk.ROOT_KEY_CLASSES_ROOT,
tryKeys[i],
wrk.ACCESS_READ);
success = true;
} catch(e) {}
}

if(!success) return;

var command = wrk.readStringValue('').match(/^(?:".+?"|[^"]\S+)/);
if(!command) return;
return command[0].replace(/"/g, '');
},

// =========================================== //
// FUNCTIONS: PDF ANNOTATION EXTRACTION CLASS //
Expand Down

0 comments on commit 1be759e

Please sign in to comment.