Help getting FileName out of theFile using AppleScript

Talk, speculate, discuss, pontificate. As long as it pertains to Hazel.

Moderators: Mr_Noodle, Moderators

Hey all,

First I want to say that Hazel is AMAZING. There is so much power behing this product and that's just awesome.

I am trying to do some stuff with AppleScript when I've finished processing a file. I am trying to get just the name of the file and not the rest of the path information. For example, here is how I am generating the string:

Code: Select all
"Processing of " & theFile & " is complete"

gives me the following:
Processing of Macintosh HD:Users:justin:Downloads:MyFile.txt is complete

Where I want to see:
Processing of MyFile.txt is complete

I'm sure that there is some way to do this, but I've spent about an hour searching through the forums and I can't seem to find it anywhere.

Thanks in advance!
bondjw07
 
Posts: 11
Joined: Sun Nov 25, 2012 4:58 pm

Google around for how to do this in applescript. There's nothing different in terms of how to do this with Hazel. That said, you can alternatively avoid the script and use either Notification Center (if you are on 10.8) or Growl in conjunction with Hazel's built-in notification action.
Mr_Noodle
Site Admin
 
Posts: 11195
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

I guess my problem is that I don't know what type of variable "theFile" is. I can't really play around with it in AppleScript Editor because theFile is obviously not there. So there really isn't anything that I can figure out to search for. Any help would be greatly appreciated!

Also, I'm sending an iMessage so that I get the message to my Cell, so Growl and Notification Center are out :)
bondjw07
 
Posts: 11
Joined: Sun Nov 25, 2012 4:58 pm

It just occurred to me that theFile is probably nothing more than a string. For some reason I was thinking it was an object that had lots of other data. I guess I'm going to have to think about this a little more....

bondjw07 wrote:I guess my problem is that I don't know what type of variable "theFile" is. I can't really play around with it in AppleScript Editor because theFile is obviously not there. So there really isn't anything that I can figure out to search for. Any help would be greatly appreciated!

Also, I'm sending an iMessage so that I get the message to my Cell, so Growl and Notification Center are out :)
bondjw07
 
Posts: 11
Joined: Sun Nov 25, 2012 4:58 pm

It's actually an alias to a file which you can convert into a path and depending, may get automatically coerced into a string. I suggest reading the AppleScript article i the help.
Mr_Noodle
Site Admin
 
Posts: 11195
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Okay, that explains a lot of the errors that I've been getting referring to an alias. I'll take a look at that article and see if I can make some more sense of what I'm trying to do. Thanks a lot!
bondjw07
 
Posts: 11
Joined: Sun Nov 25, 2012 4:58 pm

So would creating an alias of the HFS path (as seen below) be the correct way of mimicking theFile while testing in AppleScript Editor?
Code: Select all
set theFile to alias "Drobo:VideoProcessing:MKVs:Family.Guy.S11E06.720p.HDTV.X264-DIMENSION.mkv"

Thanks for all your help, my first time really getting to learn AppleScript.
bondjw07
 
Posts: 11
Joined: Sun Nov 25, 2012 4:58 pm

Doing a quick google search, I think this might work:
Code: Select all
set fileName to name of theFile
Mr_Noodle
Site Admin
 
Posts: 11195
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

I did actually figure this out yesterday. Once I understood that the file was indeed an alias and not a string, I was able to search around the net and find out how to coerce the variable:
Code: Select all
set filepath to theFile as string

Thanks again for all your help! I'll be purchasing this weekend now that I've found that I can get everything to work. Awesome product.
bondjw07
 
Posts: 11
Joined: Sun Nov 25, 2012 4:58 pm

I do it this way:

first tell the finder what you want to do -

tell application "Finder"
set filename to name of (your file)

then you can use "filename" in any later script call you like.

I use it in a metadata tagging script to email me once a file is complete and ready to view.

For instance:

tell application "Mail"
set the NewMessage to make new outgoing message with properties {subject:"Your TV Show is ready", content: "" & filename & " has been added to iTunes and is ready to view", visible:true}

and some other mail script stuff after that.
cdavis
 
Posts: 7
Joined: Tue Nov 13, 2012 6:29 pm

bondjw07 wrote:Hey all,

First I want to say that Hazel is AMAZING. There is so much power behing this product and that's just awesome.

I am trying to do some stuff with AppleScript when I've finished processing a file. I am trying to get just the name of the file and not the rest of the path information. For example, here is how I am generating the string:

Code: Select all
"Processing of " & theFile & " is complete"

gives me the following:
Processing of Macintosh HD:Users:justin:Downloads:MyFile.txt is complete

Where I want to see:
Processing of MyFile.txt is complete

I'm sure that there is some way to do this, but I've spent about an hour searching through the forums and I can't seem to find it anywhere.

Thanks in advance!


Hi,
I'm trying to reach the same goal that you seem to have reached!!
Do you mind sending the whole script that you used to send only the file name instead of the whole fiepath?
drmar
 
Posts: 1
Joined: Thu Jan 02, 2014 7:52 pm

Below is the entire script I am using to send me an iMessage when a process is complete. The message would read "Download Complete: <filename>". Where <filename> is the name of the file. Also, <10DigitPhoneNumber> is to be replaced with the phone number you want to message that is associated to iMessage.

Hope this helps!

Code: Select all
set filepath to theFile as string

tell application "Finder" to set {dispName, nameExt, isHidden} to {displayed name, name extension, extension hidden} of alias filepath
if isHidden or nameExt is equal to "" then
   dispName
else
   (characters 1 through (-2 - (count of nameExt)) of dispName) as text
end if
set baseName to result
tell application "Messages"
   set x to 0
   
   send "Download Complete: " & baseName to buddy "<10DigitPhoneNumber>" of service x
end tell
bondjw07
 
Posts: 11
Joined: Sun Nov 25, 2012 4:58 pm

Thanks for your posts, It's help me a lot :)
Snare
 
Posts: 1
Joined: Tue Oct 28, 2014 7:20 am


Return to Open Discussion