Folder Action script to check files with same name but not extension?

Hi all,

I’m trying to tweak a folder action script to something I’m not even sure it’s possible. I’m using a script provided by iFlicks - a video utility - that automatically processes movie files added to the folder.

Here’s the original script:

on adding folder items to thisFolder after receiving addedItems
repeat with movieFile in addedItems
tell application "iFlicks"
import movieFile without gui
end tell
end repeat
end adding folder items to

On that same folder, I’ll also be adding subtitle files (.srt). The movie files will be added first, though. However, I can only have the movie file sent to iFlicks if a .srt with the same name of the movie file exists. I need to modify the script so that it only runs when an .srt file is added and when there is already a .mov file (or any other video extension) with the same name of the .srt (actually, it would be even better if, on adding a .mov file or a .srt file, it would only execute the script if another file with the same name but a different extension existed - just in case the .srt files is added first).

This is what I’m trying:

on adding folder items to thisFolder after receiving addedItems
repeat with movieFile in addedItems
set {Nm, Ex} to {name, name extension} of (info for movieFile)
if Ex is "srt" then
set Nm to {Nm, Nm's text 1 thru (-2 - (count Ex))}'s item (((Nm contains ".") as integer) + 1)
set movieFile to thisFolder & Nm & ".avi"
try
set movieFile to path to movieFile as alias
tell application "iFlicks"
import movieFile without gui
end tell
end try
end if
end repeat
end adding folder items to

I was trying to pass the movieFile as a string - I realized that wouldn’t work, but I’m sure I’m doing it wrong with setting as alias. Also, I really have no idea how to check for a file with a specific name and extension.

I’m not really a scripter, but I’ve searched the forums to grab the bits I’m trying to use. Still, no luck :frowning:
Any ideias?

Thanks in advance!

Here’s a handler to get the name and extension. As such I think your script should be like this…

on adding folder items to thisFolder after receiving addedItems
repeat with movieFile in addedItems
set {Nm, Ex} to getName_andExtension(movieFile)
set movieFilePath to (thisFolder as text) & Nm & ".avi"
try
tell application "iFlicks"
import (movieFilePath as alias) without gui
end tell
end try
end if
end repeat
end adding folder items to

on getName_andExtension(F)
	set F to F as Unicode text
	set {name:Nm, name extension:Ex} to info for file F without size
	if Ex is missing value then set Ex to ""
	if Ex is not "" then
		set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
		set Ex to "." & Ex
	end if
	return {Nm, Ex}
end getName_andExtension

You’ll see that we created movieFilePath using “&” to add some strings together to form the path. Note that using “&” you can only add strings… so we made the folder, which is an alias-type structure, into a string and then added the other strings to it. Now that the path is a string we coerced it to an alias so iFlicks could use it. So now you know how to coerce an alias to a string so you can modify it and you know how to coerce a string back into an alias so you can use it.

I hope that helps.

Thanks a lot!

Newbie doubts, as you can see :slight_smile: Really didn’t know I could just use (movieFilePath as alias), thought I had to set the variable again (there’s an extra ‘end if’ on the function, but I removed that).

Now, two tiny doubts:

If I want to make sure the file being added to the folder is a .srt can I still use an if like I was trying to use? That is, if Ex is “srt” then… right after getting the name and extension? Or is this a bad practice?

Also: if I want to have a script that checks if either an .avi file or an .srt exists when adding the correspondent .avi or .srt, how exactly do I check the folder for items with the same name?

I’ve looked for an handler for that, and thought this might work:


on FinderItemExists(thePath)
    try
        set thePath to thePath as alias
    on error
        return false
    end try
    return true
end FinderItemExists

Again, thank you!

Well, I think I got it. At least it seems to work as expected :slight_smile:


on adding folder items to thisFolder after receiving addedItems
	repeat with movieFile in addedItems
		set {Nm, Ex} to getName_andExtension(movieFile)
		set movieFilePath to (thisFolder as text) & Nm & ".avi"
		set subtitleFile to (thisFolder as text) & Nm & ".srt"
		if Ex is ".srt" then
			try
				tell application "iFlicks"
					import (movieFilePath as alias) with gui
				end tell
			end try
		else if Ex is ".avi" then
			if my FinderItemExists(subtitleFile) then
				try
					tell application "iFlicks"
						import (movieFilePath as alias) with gui
					end tell
				end try
			end if
		end if
	end repeat
end adding folder items to

on getName_andExtension(F)
	set F to F as Unicode text
	set {name:Nm, name extension:Ex} to info for file F without size
	if Ex is missing value then set Ex to ""
	if Ex is not "" then
		set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
		set Ex to "." & Ex
	end if
	return {Nm, Ex}
end getName_andExtension

on FinderItemExists(thePath)
	try
		set thePath to thePath as alias
	on error
		return false
	end try
	return true
end FinderItemExists

On adding an .srt file, it checks for a .avi file with the same and sends it to iFlicks; on adding an .avi file, it checks for .srt file with the same name and, if it exists, it sends the .avi file to iFlicks - if no .srt matching file is founded, nothing.

Seems to work. Does it looks rights?
Thanks!

The logic looks good to me. Here’s a couple notes for minor improvements…

  1. this line could go inside the “else if Ex is “.avi” then” statement because you don’t really need to calculate it unless you got an avi file.
set subtitleFile to (thisFolder as text) & Nm & ".srt"
  1. again, if movieFile was an avi file then there’s no sense in coercing movieFilePath to an alias when you can just use movieFile which is already an alias… so it could be this line inside the “tell application “iFlicks”” statement.
import movieFile with gui
  1. in my handler I add the “.” onto the file extension because the last time I needed that handler I needed the period for something. You could remove this statement from that handler and then you could just check the extension without using a period.
set Ex to "." & Ex

Obviously they’re very minor things. In all I think you’ve got what you need. Good luck.

Thanks, Hank!

It’s already up and running - and working as expected, so far.

Again, thank you a lot - last night I was banging my head in despair and now I have a fully working script.

Cheers!