Check if file has a parent folder

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

Moderator: Mr_Noodle

Check if file has a parent folder Wed Jan 28, 2015 3:11 pm • by seeplanet
I have a situation where I have Hazel looking at an incoming folder. Sometimes I have files come in that are inside of a folder and sometimes they only come in as files without a folder.

I then run a rule that renames the file based on the parent folder. The problem is with the files that have no folder, they are getting renamed with the incoming folder name and this causes issues.

Does anyone know of a way to have a rule/script look at the parent folder of the file in question and do a check? For example:

If file has parent folder named "Incoming" then do nothing. Else, rename file to parent folder.

Thanks for any suggestions!
seeplanet
 
Posts: 8
Joined: Thu Nov 10, 2011 3:34 am

Re: Check if file has a parent folder Thu Jan 29, 2015 9:36 am • by seeplanet
Update: I had someone help me with an Applescript, but its giving me an error. Here is the script and below is the error I am getting. I am not sure if there is something I need to change for Hazel.

Code: Select all
set theFile to choose file
tell application "Finder"
   set parentFolderName to name of container of theFile
   if parentFolderName is not "Ready for Sorting" then
      set fileExtension to name extension of theFile
      set name of theFile to parentFolderName & "." & fileExtension
   end if
end tell


Error that I am getting:

Code: Select all
hazelworker[42081] OSAScript error: {
    NSLocalizedDescription = "No user interaction allowed.";
    NSLocalizedFailureReason = "No user interaction allowed.";
    OSAScriptErrorAppAddressKey = "<NSAppleEventDescriptor: [0x0,2 \"hazelworker\"]>";
    OSAScriptErrorAppNameKey = "Hazel.prefPane";
    OSAScriptErrorBriefMessageKey = "No user interaction allowed.";
    OSAScriptErrorMessageKey = "No user interaction allowed.";
    OSAScriptErrorNumberKey = "-1713";
    OSAScriptErrorRangeKey = "NSRange: {0, 0}";
}


Any help would be appreciated!
seeplanet
 
Posts: 8
Joined: Thu Nov 10, 2011 3:34 am

Re: Check if file has a parent folder Thu Jan 29, 2015 12:24 pm • by Mr_Noodle
How about something like:
Code: Select all
   If (all) match
        If (all) match for (the enclosing folder)
            Name is not <incoming folder name>
            Name matches (• parent name)
   Do
        Rename <some pattern using •parent name>


That will match files whose parent isn't the incoming folder name and stores that name in •parent name, where that is a custom token which matches anything.

Also, you need to tell Hazel to go into subfolders. See the sticky article at the top of this forum.

Your AppleScript isn't necessary AFAICT but for future reference, anytime you need to access any UI from AppleScript, you should bracket that code with "tell application "System Events"...end tell"
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Check if file has a parent folder Thu Jan 29, 2015 2:05 pm • by seeplanet
Thanks Mr_Noodle that did it! Thanks for the info also.
seeplanet
 
Posts: 8
Joined: Thu Nov 10, 2011 3:34 am


Return to Support