astrofin
-
Posts
7 -
Joined
-
Last visited
Content Type
Blogs
Gallery
Downloads
Events
Profiles
Forums
Articles
Media Demo
Posts posted by astrofin
-
-
Thanks deanishe
Can I please confirm that on your machine you're able to read `list.csv` if called from `script.js` using an absolute path?
I've updated `script.js` to use `var file = "~/Documents/list.csv";`. It's still not working for me, which would suggest the problem lies elsewhere rather than with the script.
It would be very useful if you're able to confirm this.
I note your suggestion to move everything into the workflow. For various reasons, `list.js` needs to be accessible outside the workflow, otherwise I would follow this suggestion.
Thanks for taking the time to help!
-
Thanks deanishe
I've put everything on GitHub. The repo contains the workflow to be installed into Alfred as usual, plus the files `script.js` and `list.csv` that should be placed in your `~/Documents` directory.
Thanks for your help
-
Thanks deanishe, I appreciate your help with this.
The following is a minimal version of the workflow that creates this issue.
The workflow is very simple for testing purposes. It consists of only a Script Filter outputting to Large Type. The Script Filer runs my script.js file, which is not located within the workflow package because Alfred is not the only way I access it. The Script Filter script field contains only:
~/Documents/script.js list
~/Documents/script.js looks like this:
#!/usr/bin/env osascript -l JavaScript ObjC.import('stdlib') ObjC.import('Foundation') const app = Application.currentApplication(); app.includeStandardAdditions = true function run(argv) { try { return selectCommand(argv) } catch (e) { return e } } function selectCommand(argv) { const cmd = argv[0] if (cmd === 'list') { return list('all') } $.exit(0) } /** * Commands */ // Build the list function list() { // init an empty object to hold the results var result = { 'items': [] } // the csv file var file = "list.csv"; // read the csv file var csv = app.read( Path(file) ) // split the csv string into an array of rows var rows = csv.split('\r\n') // get the headers row var headers = rows[0].split(','); // drop the header row rows.splice(0,1) // iterate through the rows rows.forEach( (row) => { // init an empty object to hold the results var obj = {} // split the row into an array of attributes var currentRow = row.split(',') // iterate through the headers headers.forEach( (header,i) => { // add the key/value pair to the obj container obj[header] = currentRow[i] }) obj["autocomplete"] = obj["title"] obj["subtitle"] = obj["title"] obj["arg"] = obj["uid"] // push the current row object into the results array result.items.push(obj) }) return JSON.stringify(result) }
~/Documents/list.csv looks like this:
uid,title 1,OptA 2,OptB
Alfred debugger shows
Error: Can't convert types. message:Can't convert types., errorNumber:-1700, line:44, column:23, stack:[native code]
Line 44 is
var csv = app.read( Path(file) )
Is there a better way to read the file?
-
Thanks deanishe, this has is very useful.
I have modified my script to `return JSON.stringify(result)`. When I run the script in terminal, I'm now getting the same JSON that appears to be correctly formatted for Script Filter.
$ ~/Documents/myscript.js list {"items":[{"uid":"1","title":"OptA","autocomplete":"OptA","subtitle":"OptA","arg":"1"},{"uid":"2","title":"OptB","autocomplete":"OptB","subtitle":"OptB","arg":"2"}]}
However, I'm still getting the same error.
I have tracked the source of this to the following line. I'm obtaining the data to populate the list from a csv file that is located in the same directory as script.js.
const app = Application.currentApplication(); app.includeStandardAdditions = true // path to the csv file var file = "list.csv"; // Read the file using a specific delimeter and return the results var csv = app.read( Path(fileString) )
When I run this script in the terminal, the current application is able to read the csv. When I run this from Alfred, the script appears to fail at this point.
What is the correct way to read the contents of text file?
-
I have a js file that ends with
msg = JSON.stringify(result) $.NSFileHandle.fileHandleWithStandardOutput.writeData( $.NSString.alloc.initWithString(String(msg)) .dataUsingEncoding($.NSUTF8StringEncoding) )
When I run the script from the terminal, it returns JSON that appears to be correctly formatted for Script Filter.
$ ~/Documents/myscript.js list {"items":[{"uid":"1","title":"OptA","autocomplete":"OptA","subtitle":"OptA","arg":"1"},{"uid":"2","title":"OptB","autocomplete":"OptB","subtitle":"OptB","arg":"2"}]}
In Alfred, I created a workflow that contains a script filter. `Alfred filters results` is checked, language set to `bin/bash`, and it is configured to run my JS script:
~/Documents/myscript.js list
When I type the keyword into Alfred, the workflow is displayed but no list results are returned.
Alfred debugger returns
ERROR: Script Filter to Script to Notification[Script Filter] JSON error: JSON text did not start with array or object and option to allow fragments not set. in JSON: Error: Can't convert types.
I assume this means that whatever Alfred has received is not recognised as valid JSON, is this correct?
Is there a way to interrogate Alfred to inspect what has been received into the script filter?
Or is there a better way to debug this error?
-
I'm creating my first Alfred workflow. It takes a keyword, and creates a new outgoing message in Mail.app using AppleScript. Something like this...
on alfred_script(q) set theSubject to q tell application "Mail" activate set theMessage to make new outgoing message with properties {visible:true} tell theMessage set subject to theSubject set content to theContent end tell end tell end alfred_script
This works fine when I explicitly set theContent to be a text string
set theContent to "this is my content"
However, there is more complexity and conditional statements in my actual script, and to keep things organised I want to set theContent to read from a text file.
set theContent to paragraphs of (read file "the_content.txt")
This doesn't work!
I'm very grateful for some pointers to help me get my first workflow up and running:
- Where should I place "the_content.txt"? In Alfred.alfredpreferences/workflows/user.workflow.123?
- Would I need to define the absolute path to "the_content.txt" in my AppleScript? Or will a relative path suffice?
- Am I reading "the_content.txt" correctly?
How to debug script filter that raises `JSON error: JSON text did not start with array or object and option to allow fragments not set`?
in Workflow Help & Questions
Posted
Thanks deanishe, appreciate your help with this. It's lead me to the right place.
Thanks for confirming that the script works and the full path are required. I had tried full path previously, but there was also another issue preventing it working.
I originally created list.csv from the terminal. I hadn't checked file permissions, which were very restrictive and preventing the script reading the file.
I've set permissions to something more sensible, and the script now works.
Thanks !