Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
hauselin committed Mar 21, 2021
1 parent 8de88e5 commit ed9b192
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# Alfred workflow for Obsidian

This simple [Alfred](https://www.alfredapp.com/) workflow uses [Obsidian](https://obsidian.md/)'s [URI scheme](https://publish.obsidian.md/help/Advanced+topics/Using+obsidian+URI) and JXA actions/scripts (thanks to [@hjklapp](https://github.com/hjklapp)) to open notes in their vaults. This workflow has been tested in Obsidian 0.11.4. See also [known issues](#known-issues).
This simple [Alfred](https://www.alfredapp.com/) workflow uses [Obsidian](https://obsidian.md/)'s [URI scheme](https://publish.obsidian.md/help/Advanced+topics/Using+obsidian+URI) and JXA actions/scripts (thanks to [@hjklapp](https://github.com/hjklapp)) to open notes in their vaults. This workflow has been tested in Obsidian 0.11.9. See also [known issues](#known-issues).

Available Alfred features/keywords (contributors in parentheses)
- Open a vault: `ov`,`oo` ([@hjklapp](https://github.com/hjklapp))
Expand All @@ -20,6 +20,7 @@ Available Alfred features/keywords (contributors in parentheses)
- Open inbox note with `oii` or customize your Hotkey to open inbox note.
- Turn on/off notifications when adding text to inbox note with `onotify`
- Fallback search when there are no results ([@technicalpickles](https://github.com/technicalpickles))
- Open terminal in vault directory: `oterm`
- Get help by opening [help vault](https://publish.obsidian.md/help/Index) and [forum](https://forum.obsidian.md/): `o?`

## Installation
Expand Down Expand Up @@ -130,6 +131,7 @@ If the Obsidian app isn't already opened, this workflow will always open the las
Alphabetical order

- [@hjklapp](https://github.com/hjklapp)
- [@jamiew](https://github.com/jamiew)
- [@lastobelus](https://github.com/lastobelus)
- [@ldebritto](https://github.com/ldebritto)
- [@luckman212](https://github.com/luckman212)
Expand Down
90 changes: 90 additions & 0 deletions tests/oi.js
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' })
}
47 changes: 47 additions & 0 deletions tests/scratch.js
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
}










0 comments on commit ed9b192

Please sign in to comment.