Skip to content

Instantly share code, notes, and snippets.

@macsimom
Created October 11, 2024 08:02
Show Gist options
  • Save macsimom/2b7f06db9b64dba0566c09ae582433fe to your computer and use it in GitHub Desktop.
Save macsimom/2b7f06db9b64dba0566c09ae582433fe to your computer and use it in GitHub Desktop.
AppleScript written in JavaScript to get printers from Active Directory and present them in a GUI based list in order to add via the lpadmin command
#!/usr/bin/osascript -l JavaScript
// version 1.1 - 2024 - Simon Andersen
let APP = Application.currentApplication()
APP.includeStandardAdditions = true
function readDefaults(plistfile,propertyKey) {
let theresult = undefined;
try {
theresult = APP.doShellScript("defaults read '"+plistfile+"' '"+propertyKey+"'");
}
catch (e) {
;
}
return theresult;
}
function getADDomain() {
var ADDOMAIN = undefined;
if (ADDOMAIN === undefined) {
ADDOMAIN = readDefaults("/Library/Managed Preferences/com.trusourcelabs.NoMAD.plist","ADDomain");
}
if (ADDOMAIN === undefined) {
ADDOMAIN = readDefaults("/Library/Preferences/com.trusourcelabs.NoMAD.plist","ADDomain")
}
if (ADDOMAIN === undefined) {
ADDOMAIN = readDefaults("/Library/Managed Preferences/menu.nomad.login.ad.plist","ADDomain")
}
if (ADDOMAIN === undefined) {
ADDOMAIN = readDefaults("/Library/Preferences/menu.nomad.login.ad.plist","ADDomain")
}
if (ADDOMAIN === undefined) {
throw new Error("No Active Directory domain specified.")
}
return ADDOMAIN
}
function naiveLDIFParse(lines) {
let records = [];
let newrecord = {};
let previousline = "";
for (i in lines) {
let line = lines[i];
if (line.startsWith(" ")) {
line = previousline+line.substring(1)
}
if (line.startsWith("dn: ")) {
newrecord["dn"]= line.substring(4)
}
else if (line.match(/^[a-zA-Z]+: /)) {
attrname = line.substring(0,line.search(':'));
newrecord[attrname] = line.substring(attrname.length+2);
}
if (line == "") {
if (Object.keys(newrecord).includes("dn")) {
records.push(newrecord)
}
newrecord = {}
}
previousline = line;
}
return records;
}
function main() {
var ADDOMAIN = getADDomain();
try {
let BASE = APP.doShellScript("ldapsearch -Y GSSAPI -h "+ADDOMAIN+" -s base '(objectClass=*)' defaultNamingContext | sed -Ene 's/^defaultNamingContext: //p'")
let RESULT = APP.doShellScript("ldapsearch -Y GSSAPI -h "+ADDOMAIN+" -s sub -b "+BASE+" '(objectClass=printQueue)' serverName printShareName description location driverName")
let lines = ObjC.unwrap(RESULT).split('\r');
let records = naiveLDIFParse(lines);
if (records.length == 0 ) {
APP.displayAlert("No network printers found.",{"as":"warning"});
return;
}
records.sort((a,b) => a["printShareName"].localeCompare(b["printShareName"]))
let THERESULT = APP.chooseFromList(records.map((x) => x["printShareName"]),{"withPrompt":"Please select a network printer"})
if (! THERESULT) {
console.log( "User cancelled");
return;
}
let CHOSENPRINTER = records.filter((x) => x["printShareName"] == THERESULT)[0];
let printername = CHOSENPRINTER["printShareName"];
let location = CHOSENPRINTER["location"];
APP.doShellScript("lpadmin -p '"+CHOSENPRINTER["printShareName"]+"' -D '"+printername+"' -L '"+location+"' -m 'drv:///sample.drv/generic.ppd' -v 'smb://"+CHOSENPRINTER["serverName"]+"/"+CHOSENPRINTER["printShareName"]+"' -o printer-is-shared=false -E");
APP.openLocation("file:///System/Library/PreferencePanes/PrintAndScan.prefPane");
}
catch (e) {
if (e.toString() == "Error: ldap_sasl_interactive_bind_s: Can't contact LDAP server (-1)" ) {
APP.displayAlert("Domain Controller unreachable.",{"as":"critical"});
return;
}
APP.displayAlert("An unexpected error occured.",{"as":"critical","message":e.toString()});
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment