-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
ObjC.import('stdlib'); | ||
console.log('Running script: oi') | ||
|
||
// get the current app to access the standard additions | ||
app = Application.currentApplication(); | ||
app.includeStandardAdditions = true; | ||
|
||
// get home path | ||
homepath = app.pathTo('home folder') | ||
console.log(homepath) | ||
|
||
// get inbox note path | ||
var inbox_md = $.getenv('inbox_md'); | ||
inbox_md = inbox_md.replace('~', homepath) | ||
|
||
// escape " | ||
var query = "{query}"; | ||
query = query.replaceAll('"', '\"'); | ||
|
||
// split paragraphs into separate bullet points | ||
query = query.split("@NEWLISTITEM@") | ||
console.log("query: " + query) | ||
|
||
// function to read utf | ||
// https://github.com/JXA-Cookbook/JXA-Cookbook/issues/25 | ||
ObjC.import('Foundation') | ||
const readFile = function (path, encoding) { | ||
!encoding && (encoding = $.NSUTF8StringEncoding) | ||
const fm = $.NSFileManager.defaultManager | ||
const data = fm.contentsAtPath(path) | ||
const str = $.NSString.alloc.initWithDataEncoding(data, encoding) | ||
return ObjC.unwrap(str) | ||
} | ||
|
||
// read file to append to it | ||
var filetext = readFile(inbox_md); | ||
|
||
// remove newline characters | ||
while (filetext.endsWith("\n")) { | ||
filetext = filetext.slice(0, filetext.length - 1); | ||
} | ||
if (filetext == "") { | ||
console.log("EMPTY NOTE!") | ||
} else { | ||
filetext += "\n"; // leave one newline character | ||
} | ||
|
||
console.log("CURRENT TEXT (string)"); | ||
console.log(filetext) | ||
|
||
// loop through each paragraph and append as separate bullet point | ||
var formatted_text = ''; | ||
for (i = 0; i < query.length; i++) { | ||
|
||
var text2append = query[i]; | ||
|
||
// parse string to remove extra spaces | ||
text2append = text2append.split(" ") | ||
text2append = text2append.filter(i => i.length > 0).join(" ") | ||
|
||
// add bullet point | ||
if (!query[i].startsWith("- ")) { | ||
text2append = "- " + text2append; | ||
} | ||
if (text2append == "- ") { | ||
continue; | ||
} | ||
|
||
if (!text2append.endsWith("\n")) { | ||
text2append += "\n"; | ||
} | ||
console.log("TEXT TO APPEND " + (i + 1)); | ||
console.log(text2append); | ||
formatted_text += text2append; | ||
} | ||
|
||
var final_text = filetext + formatted_text; | ||
console.log("FINAL TEXT") | ||
console.log(final_text) | ||
|
||
// write file | ||
str2write = $.NSString.alloc.initWithUTF8String(final_text); | ||
str2write.writeToFileAtomicallyEncodingError(inbox_md, true, $.NSUTF8StringEncoding, null); | ||
|
||
// notification | ||
var oinotify = $.getenv('oinotify'); | ||
if (oinotify == "on") { | ||
// https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/DisplayNotifications.html | ||
app.displayNotification("Added text to inbox", { withTitle: 'Obsidian Alfred workfow' }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
try { | ||
// var filetext = app.read(path) | ||
var filetext = readFile(inbox_md) | ||
// console.log(filetext) | ||
|
||
if (!filetext.endsWith("\n") && !filetext.endsWith("- ") && !filetext.endsWith("-")) { | ||
console.log("does not end with newline character") | ||
text2append = "\n" + text2append | ||
app.write(text2append, { to: openedFile, startingAt: app.getEof(openedFile) + 1 }) | ||
} else if (filetext.endsWith("\n")) { | ||
console.log("ends with newline character") | ||
console.log('remove newlines') | ||
while (filetext.endsWith("\n")) { | ||
filetext = filetext.slice(0, filetext.length - 1) // remove newline characters | ||
} | ||
filetext += ("\n" + text2append) | ||
console.log(filetext) | ||
app.write(filetext, { to: openedFile, startingAt: 0 }) | ||
} else if (filetext.endsWith("- ")) { | ||
console.log("ends with hyphen") | ||
text2append = text2append.slice(2) | ||
console.log(text2append) | ||
app.write(text2append, { to: openedFile, startingAt: app.getEof(openedFile) + 1 }) | ||
} else if (filetext.endsWith("-")) { | ||
console.log("ends with hyphen") | ||
text2append = text2append.slice(1) | ||
console.log(text2append) | ||
app.write(text2append, { to: openedFile, startingAt: app.getEof(openedFile) + 1 }) | ||
} | ||
app.closeAccess(openedFile) | ||
done = 1 | ||
} | ||
catch (error) { // empty file without any characters | ||
app.write(text2append, { to: openedFile }) | ||
app.closeAccess(openedFile) | ||
done = 1 | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|