Find Indexed Folder's Child

Like is possible in Hazel, I would to run a script on an indexed folder that finds the name of a child folder that a scanned image is put into and then this is used as a tag

The folder structure is:
Scanned Documents
—Personal
—Business1
—Business 2

e.g. “\Scanned Documents\Personal” I am getting this from the location function

I am trying to do this with applescript and awk, but I am failing. Firstly is there a better way than this and if not are there any regex gurus who can find out what I am doing wrong whilst trying to parse this into applescript.

This code works in shell script:
echo "\Scanned Documents\Personal\" | awk -F"/" '{ print$NF }'

But this does not work in applescript…

set filename to "/Scanned Documents/Personal/"
set thecommandstring to "echo \"" & filename & "\" | awk -F \"/\" '{ print$NF }'" as string
set awkResult to do shell script thecommandstring

In what way? Do you get a wrong result, no result or …?

Well, it does not here. And the code looks weird. ...Personal\" effectively escapes the quote, so the whole command is missing a quote (i.e. the first one is never closed). The path separator on macOS is /, not . So the script should not do anything at all. And I’m not sure about print$NF containing no space.

With this echo "/Scanned Documents/Personal/" | awk -F"/\//" '{ print $NF }' I’m at least getting the input string back again.

Actually, I have no idea what you’re trying to achieve. In your post, you speak about finding “the name of a child folder that a scanned image is put into”, but then I do not see a scanned image anywhere in your code. I seem to understand that you want to get the last component of the path. But I may be wrong, of course.

Assuming that this is what you want, I’d do this in JavaScript (and I have no idea how to do it in AppleScript)

const fname = "/Scanned Documents/Personal/"
const components = fname.split('/');
const lastDir = components[components.length-1];

This is not tested, though. Could be reduced to a single line if you do not need the components of the path. In any case, going down the shell way seems to be overkill to me.

Having said that, we’re on macOS, and its shell is now zsh, so this might help you to achieve what you want with zsh alone.

Thanks for that @chrillek yes not my finest coding moment! I agree in the cold llight of day this does not work!

So the essence of my problem is that when I find the location of a file in a subfolder it returns /Scanned Documents/Personal/ for example. I want to pull the final folder i.e. Personal and save that as a tag.

I wanted to do in applescript and I appreciate that this is not your weapon of choice, and thanks for your javascript suggestion but I would prefer to do in Applescript (and not to go into awk or sed if possible) if anyone has any suggestions?

I managed to crack this in Applescript…

on performSmartRule(theRecords)
	set TID to text item delimiters
	set text item delimiters to {"/"}
	
	tell application id "DNtp"
		repeat with theRecord in theRecords
			set theRecord_Location_Name to location of theRecord
			set folderTag to last text item of theRecord_Location_Name
			if folderTag = "" then set folderTag to text item -2 of theRecord_Location_Name
			set tags of theRecord to folderTag
			
		end repeat
	end tell
	set text item delimiters to TID
end performSmartRule

Now whenever a file is place in a child folder in the scanned docs 2 folder it will get a tag name of that child folder. This code sits as a Smart Rule on the Scanned Docs 2 folder

So when I scan files I just drop them in the relevant folder and they get the correct Tag

:+1:
I was going to mention set text delimiter, but then the server went awol… good that you found it