Finder + Things (CulturedCode) Integration

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Finder + Things (CulturedCode) Integration Thu Jan 11, 2018 8:50 am • by Nestorito
Hello,
I'm almost a newbie here.
I'm trying to let automagically interact Things (ToDo list app) with the finder through an hazel rule.
A typical situation is: I receive a file in mail and I want to remember to process it (read, sign or whatever).
The workflow ideally would be:
- while saving the file adding a finder tag (for hazel to process the file)
- running an applescript that:
1.get the name of the file
2. get the file path of the file to create a clickable link in things notes
3. open a dialog to choose the prefix for the name of the todo in things (i.e.: choose between "read", "sign", "whatever"

I'm not a scripter but I managed to write a script that works when run outside hazel:
Code: Select all
--set actionChoices to {"1. Leggi:", "2. Studia:", "3. Process:"}
--set theChoice to choose from list actionChoices with prompt "Cosa --vuoi fare con questo file?:" default items {"Leggi:"}
--theChoice
--set myChoice to theChoice as text

tell application "Finder"
   set theFile to the selection as text
   set finderSelList to selection as alias list
   
   if finderSelList ≠ {} then
      tell application "System Events"
         if quit delay ≠ 0 then set quit delay to 0
         repeat with i in finderSelList
            set contents of i to name of (contents of i)
         end repeat
      end tell
      
      set AppleScript's text item delimiters to linefeed
      finderSelList as text
   end if
   
   set the clipboard to "file://" & POSIX path of theFile
   tell application "Things3"
      set newToDo to make new to do ¬
         with properties {name:"Process:" & finderSelList, notes:the clipboard}
   end tell
end tell


The first commented lines define the prefix but I guess user interaction in scripts in hazel is not allowed (I got an error message about this), am I right?
So without user interaction the script simply creates a ToDo in Things which appears at it follows:

Process: NAME OF THE FILE
[Notes] clickable link in the form of "file:///Users/whatevere/mypdf.pdf


The point is that if I run the script from an hazel rule I get an error message: "Can't make POSIX path of \"\" "
So two questions:
- What am I missing?
- some real scripter can write a better code? For example I was not able to find a way to create a more "elegant" name for the clickable link instead of the file path....

Thanks, any help appreciated
:shock:
Nestorito
 
Posts: 6
Joined: Thu Sep 26, 2013 10:31 am

You can use something like "tell application System Events...end tell" to perform anything that needs a GUI, like bringing up a dialog.

As for your error, it looks like you are setting theFile to whatever happens to be selected in Finder which is incorrect. theFile is already set to an alias of the file being processed. Using whatever is selected in Finder at the time the script runs is probably asking for trouble.

Beyond that, I suggest outputting variables along the way to debug.
Mr_Noodle
Site Admin
 
Posts: 11240
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Mr_Noodle wrote:You can use something like "tell application System Events...end tell" to perform anything that needs a GUI, like bringing up a dialog.

As for your error, it looks like you are setting theFile to whatever happens to be selected in Finder which is incorrect. theFile is already set to an alias of the file being processed. Using whatever is selected in Finder at the time the script runs is probably asking for trouble.

Beyond that, I suggest outputting variables along the way to debug.


Thanks Mr_Noodle, I'll made a try...
Nestorito
 
Posts: 6
Joined: Thu Sep 26, 2013 10:31 am

Hello,
here it is my first attempt that seems to work:

Code: Select all
tell application "Finder"
   set myName to name of theFile
   set myURL to URL of theFile
   
   tell application "Things3"
      set newToDo to make new to do with properties {name:"Process: " & myName, notes:myURL}
   end tell
end tell


CAVEAT: use at your own risk, no debugging

According to a condition (in my case a tag), when the script runs it makes a new Things3 to do in the following format:
"Process: nameofthefile" and with a clickable link in the notes of the new todo.
The clickable link text is in the format "file:///etc.etc."; it could be made more 'elegant' showing the name of the file instead of its location but I haven't figured out how to do it (I guess it could be done with some html scripting and a shell script)
It could also be nice to output a notification
Nestorito
 
Posts: 6
Joined: Thu Sep 26, 2013 10:31 am


Return to Support