{"version":3,"sources":["../src/index.js"],"names":["script","opts","osascriptArgs","Object","assign","stdio","osascriptExecAsync","osascriptSpawnAsync","appName","zeroMeansNo","JSON","stringify","trim","isAppRunningAsync","e","safeIdOfAppAsync","dir","activate","openFinderToFolderAsync","pth","cmd","resolve","openInAppAsync","listOfAppNames","runningAwaitables","appIdAwaitables","push","running","Promise","all","appIds","i","length","chooseAppAsync","preferredEditor","appId","console","warn","editorsToTry","chooseEditorAppAsync","chooseTerminalAppAsync","log","openInEditorAsync","inspect","openItermToSpecificFolderAsync","inTab","openTerminalToSpecificFolderAsync","program","openFolderInTerminalAppAsync","isArray","args","line","module","exports","execAsync","spawnAsync"],"mappings":"AAAA;;;AAGA;;;;;;;6CAqBA,WAAkCA,MAAlC,EAA0CC,IAA1C,EAAgD;AAC9C,WAAO,MAAM,yBACX,WADW,EAEXC,cAAcF,MAAd,CAFW,EAGXG,OAAOC,MAAP,CAAc,EAAEC,OAAO,SAAT,EAAd,EAAoCJ,IAApC,CAHW,CAAb;AAKD,G;;kBANcK,kB;;;;;;8CAQf,WAAmCN,MAAnC,EAA2CC,IAA3C,EAAiD;AAC/C,WAAO,MAAM,0BAAW,WAAX,EAAwBC,cAAcF,MAAd,CAAxB,EAA+CC,IAA/C,CAAb;AACD,G;;kBAFcM,mB;;;;;;8CAIf,WAAiCC,OAAjC,EAA0C;AACxC,QAAIC,cAAc,CAAC,MAAMH,mBACvB,+DACEI,KAAKC,SAAL,CAAeH,OAAf,CAFqB,CAAP,EAGfI,IAHe,EAAlB;AAIA,WAAOH,gBAAgB,GAAvB;AACD,G;;kBANcI,iB;;;;;;8CAQf,WAAgCL,OAAhC,EAAyC;AACvC,QAAI;AACF,aAAO,CAAC,MAAMF,mBACZ,eAAeI,KAAKC,SAAL,CAAeH,OAAf,CADH,CAAP,EAEJI,IAFI,EAAP;AAGD,KAJD,CAIE,OAAOE,CAAP,EAAU;AACV,aAAO,IAAP;AACD;AACF,G;;kBARcC,gB;;;;;;8CAUf,WAA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AKfO,mBALe;AAMfK,yBANe;AAOf8B,8BAPe;AAQf1B,gBARe;AASfoB,mBATe;AAUfE,gCAVe;AAWfE,mCAXe;AAYf/B,kBAZe;AAafwC,cAAYhD;AAbG,CAAjB","file":"index.js","sourcesContent":["/**\n * Utilities for working with `osascript` which runs AppleScript on Macs\n */\n'use strict';\n\nimport execAsync from 'exec-async';\nimport path from 'path';\nimport spawnAsync from '@expo/spawn-async';\nimport util from 'util';\n\nfunction osascriptArgs(script) {\n if (!util.isArray(script)) {\n script = [script];\n }\n\n let args = [];\n for (let line of script) {\n args.push('-e');\n args.push(line);\n }\n\n return args;\n}\n\nasync function osascriptExecAsync(script, opts) {\n return await execAsync(\n 'osascript',\n osascriptArgs(script),\n Object.assign({ stdio: 'inherit' }, opts)\n );\n}\n\nasync function osascriptSpawnAsync(script, opts) {\n return await spawnAsync('osascript', osascriptArgs(script), opts);\n}\n\nasync function isAppRunningAsync(appName) {\n let zeroMeansNo = (await osascriptExecAsync(\n 'tell app \"System Events\" to count processes whose name is ' +\n JSON.stringify(appName)\n )).trim();\n return zeroMeansNo !== '0';\n}\n\nasync function safeIdOfAppAsync(appName) {\n try {\n return (await osascriptExecAsync(\n 'id of app ' + JSON.stringify(appName)\n )).trim();\n } catch (e) {\n return null;\n }\n}\n\nasync function openFinderToFolderAsync(dir, activate = true) {\n await osascriptSpawnAsync([\n 'tell application \"Finder\"',\n 'open POSIX file ' + JSON.stringify(dir),\n (activate && 'activate') || '',\n 'end tell',\n ]);\n}\n\nasync function openInAppAsync(appName, pth) {\n let cmd =\n 'tell app ' +\n JSON.stringify(appName) +\n ' to open ' +\n JSON.stringify(path.resolve(pth));\n // console.log(\"cmd=\", cmd);\n return await osascriptSpawnAsync(cmd);\n}\n\nasync function chooseAppAsync(listOfAppNames) {\n let runningAwaitables = [];\n let appIdAwaitables = [];\n for (let appName of listOfAppNames) {\n runningAwaitables.push(isAppRunningAsync(appName));\n appIdAwaitables.push(safeIdOfAppAsync(appName));\n }\n let running = await Promise.all(runningAwaitables);\n let appIds = await Promise.all(appIdAwaitables);\n\n let i;\n for (i = 0; i < listOfAppNames.length; i++) {\n if (running[i]) {\n return listOfAppNames[i];\n }\n }\n\n for (i = 0; i < listOfAppNames.length; i++) {\n if (!!appIds[i]) {\n return listOfAppNames[i];\n }\n }\n\n return null;\n}\n\nasync function chooseEditorAppAsync(preferredEditor) {\n if (preferredEditor) {\n // Make sure this editor exists\n let appId = await safeIdOfAppAsync(preferredEditor);\n if (appId) {\n return preferredEditor;\n } else {\n console.warn(\n `Your preferred editor (${preferredEditor}) isn't installed on this computer.`\n );\n }\n }\n\n let editorsToTry = [\n 'Visual Studio Code',\n 'Atom',\n 'Sublime Text',\n 'TextMate',\n 'TextWrangler',\n 'Visual Studio Code',\n 'Brackets',\n 'SubEthaEdit',\n 'BBEdit',\n 'Textastic',\n 'UltraEdit',\n 'MacVim',\n 'CodeRunner 2',\n 'CodeRunner',\n 'TextEdit',\n ];\n\n return await chooseAppAsync(editorsToTry);\n}\n\nasync function chooseTerminalAppAsync() {\n return await chooseAppAsync([\n 'iTerm 3',\n 'iTerm 2',\n 'iTerm',\n 'HyperTerm',\n // 'Cathode',\n // 'Terminator',\n // 'MacTerm',\n 'Terminal',\n ]);\n}\n\nasync function openInEditorAsync(pth, preferredEditor) {\n let appName = await chooseEditorAppAsync(preferredEditor);\n console.log('Will open in ' + appName + ' -- ' + pth);\n return await openInAppAsync(appName, pth);\n}\n\nasync function openItermToSpecificFolderAsync(dir) {\n return await osascriptSpawnAsync([\n 'tell application \"iTerm\"',\n 'make new terminal',\n 'tell the first terminal',\n 'activate current session',\n 'launch session \"Default Session\"',\n 'tell the last session',\n 'write text \"cd ' + util.inspect(dir) + ' && clear\"',\n // 'write text \"clear\"',\n 'end tell',\n 'end tell',\n 'end tell',\n ]);\n // exec(\"osascript -e 'tell application \\\"iTerm\\\"' -e 'make new terminal' -e 'tell the first terminal' -e 'activate current session' -e 'launch session \\\"Default Session\\\"' -e 'tell the last session' -e 'write text \\\"cd #{value}\\\"' -e 'write text \\\"clear\\\"' -e 'end tell' -e 'end tell' -e 'end tell' > /dev/null 2>&1\")\n}\n\nasync function openTerminalToSpecificFolderAsync(dir, inTab = false) {\n if (inTab) {\n return await osascriptSpawnAsync([\n 'tell application \"terminal\"',\n 'tell application \"System Events\" to tell process \"terminal\" to keystroke \"t\" using command down',\n 'do script with command \"cd ' +\n util.inspect(dir) +\n ' && clear\" in selected tab of the front window',\n 'end tell',\n ]);\n } else {\n return await osascriptSpawnAsync([\n 'tell application \"terminal\"',\n 'do script \"cd ' + util.inspect(dir) + ' && clear\"',\n 'end tell',\n 'tell application \"terminal\" to activate',\n ]);\n }\n}\n\nasync function openFolderInTerminalAppAsync(dir, inTab = false) {\n let program = await chooseTerminalAppAsync();\n\n switch (program) {\n case 'iTerm':\n return await openItermToSpecificFolderAsync(dir, inTab);\n break;\n\n case 'Terminal':\n default:\n return await openTerminalToSpecificFolderAsync(dir, inTab);\n break;\n }\n}\n\nmodule.exports = {\n chooseAppAsync,\n chooseEditorAppAsync,\n chooseTerminalAppAsync,\n execAsync: osascriptExecAsync,\n isAppRunningAsync,\n openFinderToFolderAsync,\n openFolderInTerminalAppAsync,\n openInAppAsync,\n openInEditorAsync,\n openItermToSpecificFolderAsync,\n openTerminalToSpecificFolderAsync,\n safeIdOfAppAsync,\n spawnAsync: osascriptSpawnAsync,\n};\n"]}