Conditional sort into subfolder

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

Moderator: Mr_Noodle

Conditional sort into subfolder Sat Nov 02, 2013 9:55 am • by raghav
Hi,
Is there a way to put a conditional check before initiating sort into subfolders - based on the value of a variable?

I have a rule which runs an apple script on PDF files and the script attempts to categorize the files based on certain keywords & returns two variables 'category' & 'subcategory' to hazel. Eventual goal is to sort PDF files into a directory structure of 'Category'/'Subcategory'/.
however, for cases where subcategorization fails, the script returns "Uncategorized".
is there a way to check this return value for "Uncategorized" & then not initiate the second level 'sort into subfolder'?
thanks
Raghav
raghav
 
Posts: 16
Joined: Fri Oct 19, 2012 10:16 am

Re: Conditional sort into subfolder Mon Nov 04, 2013 4:20 pm • by Mr_Noodle
Search the help for AppleScript as there is a value you can return to tell Hazel to stop executing actions. If you return that, any actions after that script will not be executed.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Conditional sort into subfolder Wed Nov 06, 2013 10:15 am • by raghav
If i got it right, you are suggesting me to add a script between the first and the second 'sort' actions & the script can return hazelstop to indicate when to stop. Right?

here's what my rule looks like:
if kind is PDF
do
1. run external applescript to determine the category and subcategory (script exports these as tokens)
2. move to main folder
3. sort into sub folder "category"
4. sort into sub folder "subcategory"

I tried adding an embedded script between [3] & [4]. However, i need to refer to the token subcategory in this script. is that possible? referring to a custom token exported by an external script in another embedded script?
currently i'm getting following error when the rule runs

2013-11-06 19:34:55.126 hazelworker[2949] AppleScript error: {
NSLocalizedDescription = "The variable ctkSubcategoryTag is not defined.";
NSLocalizedFailureReason = "The variable ctkSubcategoryTag is not defined.";
OSAScriptErrorAppAddressKey = "<NSAppleEventDescriptor: null()>";
OSAScriptErrorBriefMessageKey = "The variable ctkSubcategoryTag is not defined.";
OSAScriptErrorMessageKey = "The variable ctkSubcategoryTag is not defined.";
OSAScriptErrorNumberKey = "-2753";
OSAScriptErrorOffendingObjectKey = "<NSAppleEventDescriptor: 'utxt'(\"ctkSubcategoryTag\")>";
OSAScriptErrorRangeKey = "NSRange: {0, 0}";
raghav
 
Posts: 16
Joined: Fri Oct 19, 2012 10:16 am

Re: Conditional sort into subfolder Wed Nov 06, 2013 4:29 pm • by Mr_Noodle
Why not put it in the first script and exchange steps 1 & 2? As for that error, that seems to be specific to your script.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Conditional sort into subfolder Thu Nov 07, 2013 12:04 am • by raghav
first script figures out the category and subcategory for a file.
step 1 is mandatory - all PDFs get moved to one folder after downloading.
step 2 is very likely - every PDF will be very likely to belong to a particular "category" and gets moved to the folder named "category"
step 3 - this is the key thing here. some PDFs may not belong to any subcategory and hence need to remain in "category" folder. if a valid subcategory is found, then it gets moved into "category"/"subcategory" folder.

so the first script cannot return hazelstop - because that will stop the PDF from getting moved to the "category" folder in the first place. what i'm looking for is a way to check some variable/token before initiating the step 3.
raghav
 
Posts: 16
Joined: Fri Oct 19, 2012 10:16 am

Re: Conditional sort into subfolder Thu Nov 07, 2013 3:09 pm • by Mr_Noodle
But if the move is mandatory, make that the first step. That way the second step, where you run the script can return hazelstop if you determine there are no categories.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Conditional sort into subfolder Thu Nov 07, 2013 10:54 pm • by raghav
yeah. the mandatory move is already the first step - moving any newly saved PDF into one common folder say "PDF folder". i hope a sample example can make it clear.

ex: lets say i receive two PDFs in mail with the names "Bill-CC-11112013-Statement.PDF" & "Bill-Phone-Nov2013.PDF".
firstly, both these pdfs get hazeled into a common folder "AllPDFs".
next, to find out further categories, a script gets executed. This script figures out that both files are "Bills". It further figures out that one is a "CreditCard" statement while the other is a "Telephone" bill. so it returns "Bill" as category and "CreditCard" or "Telephone" as subcategory.
now the PDF first gets subsorted into "Bills" and then further subsorted into "Bills/CreditCard" or "Bills/Telephone".
However, there would be cases where the script does not see any pattern for sub-categorization. in such cases, it'd return category as "Bills" & subcategory as "Uncategorized".
I want to check the custom token for subcategory returned by the script to see if it is "Uncategorized" before subsorting it within the "Bills" folder. if it is, then i will let it remain in "Bills" folder and not initiate the second sub-sorting.

Note: above is one example. there are many other "category"/"Subcategory" that the script figures out parsing through a database.
raghav
 
Posts: 16
Joined: Fri Oct 19, 2012 10:16 am

Re: Conditional sort into subfolder Fri Nov 08, 2013 11:48 am • by Mr_Noodle
Right, so why can't you have the script that already exists to determine the sub-category return hazelstop if there is no category thus stopping the rule before doing the actual sorting?
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Conditional sort into subfolder Fri Nov 08, 2013 11:01 pm • by raghav
because the existing script figures out both "category" and "subcategory" in one go.
in the scenario that i'm talking about, "category" will be valid but subcategory will be "uncategorized".
so i need the first subfolder sort (to move the file to folder "category") to work before the script can stop. this is why the script cannot return hazelstop immediately.
raghav
 
Posts: 16
Joined: Fri Oct 19, 2012 10:16 am

Re: Conditional sort into subfolder Mon Nov 11, 2013 3:49 pm • by Mr_Noodle
Ah, I see. For the moment, you'll have to figure out a way to communicate between the scripts. Or, have you tried returning a blank string for the second category and seeing whether the sort action can handle that?
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Conditional sort into subfolder Mon Nov 11, 2013 8:54 pm • by raghav
yeah, for now i eventually got what i had wanted by making sure that the second subsort fails - by modifying the script.
if there is a wishlist, i'd like to see one of the below mechanisms
1. ability to send custom tokens returned by one script to another script
or
2. a mechanism to insert a sort of "if" condition even within actions. I know that this brings in lot of complexities but a simple custom token check mechanism to begin with?

thanks
raghav
 
Posts: 16
Joined: Fri Oct 19, 2012 10:16 am

Re: Conditional sort into subfolder Wed Nov 13, 2013 11:53 am • by Mr_Noodle
#1 is already on the feature list. #2 is probably not going to happen as it would make things way too complicated.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Conditional sort into subfolder Wed Nov 13, 2013 12:51 pm • by raghav
Are you saying that #1 is already on the list of features planned for future releases?. is it planned for any time soon?
On a different note, i was wondering if there is a way to assign a 'mirror' folder for any folder such that any action done on the main folder is reflected in 'mirror' folder.
usecase : to have automatic backup/sync between two folders - as soon as a file is copied over to the main folder, it also gets synced to the 'mirror' folder.
I must admit i still haven't used "sync" action yet - so still in the initial stages of figuring out how to get this done.

thanks for the support & this software - hazel tops my list of best-bang-for-the-buck softwares!
raghav
 
Posts: 16
Joined: Fri Oct 19, 2012 10:16 am

Re: Conditional sort into subfolder Wed Nov 13, 2013 12:53 pm • by Mr_Noodle
#1 is on the list but I can't say when it will be implemented.

For syncing, check out this thread: viewtopic.php?f=4&t=1593#p6500
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Support