Getting a file object from a file path

I can’t believe how hard this is - I know four other languages and I’m stuck on a freaking filehandle.

My script eventually comes up with a filepath, the kind with the colons in it.

I want to test it either for its file extension or file type.

				
set theAttachment to item theAttachmentIndex of theAttachments
				set theAttachmentFileName to name of theAttachment
				set attFile to "tmp:" & theAttachmentFileName
				save theAttachment in attFile
				--set aFile to attFile as file
				
				--set theFile to attFile as file specification

the file specification one works, but it doesn’t give me what I want. The file one doesn’t work. And getting info from either hasn’t been fruitful.

What I eventually want to do is test to see if the extension is aiff, and if it is, call afconvert to convert the file to a CAF file. But first I need to know how to get a file object from a file path. Can anyone help?

Thanks!

Hi,

try this


.
set theAttachment to item theAttachmentIndex of theAttachments
set theAttachmentFileName to name of theAttachment
set attFile to "tmp:" & theAttachmentFileName
save theAttachment in attFile

set aFile to attFile as alias
set Ex to file extension of (info for aFile)
if Ex is "aiff" then
	-- do something
end if

Nope, no luck. It seems to stop processing at:

set aFile to attFile as alias

the context is missing.
Are you talking about Mail.app?
With your syntax “tmp” must be the name of a volume

Sorry about that, yes, this is an applescript that is called from Mail.app to, among other things, extract attachments from a mail message. I have an iPhone app that mails a voice recording to me, and then I want this rule to extract the voice recording and do something else with it.

However, I experimented with the file path and when prepending a volume it wouldn’t save. Since it’s saving in /tmp I thought that would be sufficient.

At any rate, when I reconstruct the path to prepend a Volume, it still isn’t working:



		set theAttachments to mail attachments of the theMessage
		set theAttachmentCount to count of theAttachments

			repeat with theAttachmentIndex from 1 to theAttachmentCount
				set theAttachment to item theAttachmentIndex of theAttachments
				set theAttachmentFileName to name of theAttachment

                                set attFile to "tmp:" & theAttachmentFileName
				save theAttachment in attFile
				
				set filePath to "Juggernaut:tmp:" & theAttachmentFileName
				set aFile to filePath as alias
				set Ex to file extension of (info for aFile)

It cannot work, because you’re going to save the attachment to a non existing path


.
	set theAttachments to mail attachments of the theMessage
	set theAttachmentCount to count of theAttachments
	
	repeat with theAttachmentIndex from 1 to theAttachmentCount
		set theAttachment to item theAttachmentIndex of theAttachments
		set theAttachmentFileName to name of theAttachment
		
		set filePath to "Juggernaut:tmp:" & theAttachmentFileName
		save theAttachment in filePath
		
		set aFile to filePath as alias
		set Ex to name extension of (info for aFile)
	.

sorry, it’s name extension not file extension

Oh, the file has been saving successfully just fine. That part of the code has been working. It fails here:

   set aFile to filePath as alias

I’ll play with it some more after some sleep. Thanks for your help so far!

I’ve just tested the script.
If the attachment has really been saved to the destination location, the alias coercion works

Hello,

I don’t know if this will help you, but once you have a folder with your attachments in it you can use something like

set docsToConvert to every file of folder MyFolderOfAttachments whose name ends with ".aiff"

→ That should generate a list of all the .aiff files in that folder.

You can then step through this list and process the files.

Regards,

  • Zoar