Configure Folder to 'Act Like' File When Opened

Hi All,

Expertise Level: I am new to apple scripting but not programming.

I am trying to configure a folder (when double clicked) to open a file contained in the folder. I know this sounds silly, but my intentions are to hide unnecessary contents of the folder for the user. For instance lets say I have a movie folder for ‘Saved by the Bell: Hawaiian Style’ (greatest movie ever made, jk). In this folder I have the movie (SavedByTheBell.mpeg) and other information such as subtitles (SavedByTheBell.srt), etc. The movie needs this information in the subdirectories but this is not something the user needs to know. In other, words from user prespective its better if the folder, when opened, opens just the movie file (SavedByTheBell.mpeg).

What’s more, I want to make a generic script that opens the *.mpeg file contained in any folder I specify (ie. so I can use script for other movie folders). What I’m trying to do is somewhat like apple has ‘packaged’ its applications. Please give me suggestions.

Cheers,
ScKaSx
MacBook Pro Intel 10.4.11

Use a folder action then attach a script to the folder. Something like this script should do it for you. Then I guess you could even give the folder a new icon which looks like a movie file, then the user wouldn’t even realize they were opening a folder.

property movieExtensions : {"mov", "avi", "mpg", "mpeg"}

on opening folder this_folder
	tell application "Finder"
		close window 1
		set theFiles to every file of this_folder
		repeat with aFile in theFiles
			if name extension of aFile is in movieExtensions then
				tell application "QuickTime Player"
					activate
					open aFile
				end tell
				exit repeat
			end if
		end repeat
	end tell
end opening folder

This should get you started.
I would do a search for Folder Actions and familiarize yourself
with their use.

Save this script in:
“COMPUTER_NAME:Library:Scripts:Folder Action Scripts:”

Then attach it to the folder containing your files.
You do that by right mouse clicking on the folder
choosing More at the bottom and then Configure Folder Actions

You will then attach the saved script to that folder.
You will need to do this on each folder you wish
to behave in this manner.

A more elegant workflow might be to store all your files
in a Movies folder in the Application Support folder.
Then have a script that allows the user to Choose From List the movies contained
in that folder.

hth,

Craig


on opening folder this_folder
	tell application "Finder"
		close folder this_folder
		set theFile to every file of folder this_folder whose name extension is "mpeg"
		open theFile
	end tell
end opening folder

Thanks guys! Your suggestions were great and they work brilliantly. I just had afew follow up questions.

Is there any way to keep the movies folder open after the script. In both your scripts you execute

I was just wondering if there is a function like ‘back folder’ or something, so I don’t have to close the folder.

Also, Craig mentioned that storing the files in Application Support would be more elegant. I was just curious why you would prefer this method over having the movie files stored in the movies directory under user. I realize that adding folder actions for each movie is alittle cumbersome but even then, why put the files in ‘Appication Support’?

Also, I think regulus6633 was reading my mind. I made icons for the folders of the movies and then decided to write a script for them. As it is, it is pretty cool, thanks to you guys.

Cheers,
ScKaSx

Yes, just remove the line close folder this_folder

Although this idea is cool, another way for you to achieve the same thing (but maybe a better way) is to do as craig suggests and keep these folders out of the users way by placing them in the application support folder, and then just keeping an alias to the movies files in the users movie folder.

Here’s a script that will make the movie alias files for you. This assumes you have the movie folders located in a folder called “Movie Folders” in the “Application Support” folder.

property movieExtensions : {"3g2", "3gp", "3gp2", "3gpp", "3mm", "60d", "aep", "ajp", "amv", "asf", "asx", "avb", "avi", "avs", "bik", "bix", "box", "byu", "cvc", "dce", "dif", "dir", "divx", "dv", "dvr-ms", "dxr", "eye", "fcp", "flc", "fli", "flv", "flx", "gl", "grasp", "gvi", "gvp", "ifo", "imovieproject", "ivf", "ivs", "izz", "izzy", "lsf", "lsx", "m1v", "m2v", "m4e", "m4u", "m4v", "mjp", "mkv", "moov", "mov", "movie", "mp4", "mpe", "mpeg", "mpg", "mpv2", "msh", "mswmm", "mvb", "mvc", "nvc", "ogm", "omf", "prproj", "prx", "qt", "qtch", "rm", "rmvb", "rp", "rts", "sbk", "scm", "smil", "smv", "spl", "srt", "ssm", "svi", "swf", "swi", "tivo", "ts", "vdo", "vf", "vfw", "vid", "viewlet", "viv", "vivo", "vob", "vro", "wm", "wmd", "wmv", "wmx", "wvx", "yuv"} -- found here http://www.fileinfo.net/filetypes/video

set moviesFoldersPath to (path to application support folder from user domain as text) & "Movie Folders:"
set moviesAliasPath to path to movies folder

tell application "Finder"
	-- remove all the alias files from the movies folder
	set aliasFiles to every file of moviesAliasPath whose class is alias file
	repeat with anAlias in aliasFiles
		move anAlias to trash
	end repeat
	
	-- get all of the movie files from the movies folders in the application support folder
	set movieFiles to files of entire contents of folder moviesFoldersPath whose name extension is in movieExtensions
	
	-- make an alias file for each movie in the movie folder
	repeat with aMovieFile in movieFiles
		make new alias file at moviesAliasPath to aMovieFile
	end repeat
end tell

Thanks for the reply regulus.

I see, so basically it would be easier to just hide the folder in Application Support and have a list of alias’s in ‘User/ComputerName/movie folder’. I can see your point. Thanks for the script too, that should make it easy when I add more movies to App Support!

Cheers,
ScKaSx

Hi Guys,

I’m being nit-picky, but the solution I’m hovering towards is putting my movies in Application Support and Aliasing the movie files to /User/CompName/Movies. However, I don’t like the arrows created by the aliasing, especially since I am using my own icons for the movie files.

Is there a way to disable the aliasing arrows?

Thanks.

Cheers,
ScKaSx

Not that I know of.

Well, I had another thought. You could use a unix link file instead of an alias file. You can look at the man page for ln if you don’t know what they are, but basically they act just like alias’s. Note that this only works if the real movies and the link file are on the same drive (which should work for you). If they’re not on the same drive then it won’t work.

One part you need to be careful of… the part where we “remove all the alias files from the movies folder”… this will now move any movie file to the trash, including other movie files that a user may have in their movie folder other than the ones we place in this folder. So if you’re worried about that then you’ll need to find another method to trash the old link files before the new ones are created.

So I made a few mods to the script, although I didn’t test it. Let me know if it works.

property movieExtensions : {"3g2", "3gp", "3gp2", "3gpp", "3mm", "60d", "aep", "ajp", "amv", "asf", "asx", "avb", "avi", "avs", "bik", "bix", "box", "byu", "cvc", "dce", "dif", "dir", "divx", "dv", "dvr-ms", "dxr", "eye", "fcp", "flc", "fli", "flv", "flx", "gl", "grasp", "gvi", "gvp", "ifo", "imovieproject", "ivf", "ivs", "izz", "izzy", "lsf", "lsx", "m1v", "m2v", "m4e", "m4u", "m4v", "mjp", "mkv", "moov", "mov", "movie", "mp4", "mpe", "mpeg", "mpg", "mpv2", "msh", "mswmm", "mvb", "mvc", "nvc", "ogm", "omf", "prproj", "prx", "qt", "qtch", "rm", "rmvb", "rp", "rts", "sbk", "scm", "smil", "smv", "spl", "srt", "ssm", "svi", "swf", "swi", "tivo", "ts", "vdo", "vf", "vfw", "vid", "viewlet", "viv", "vivo", "vob", "vro", "wm", "wmd", "wmv", "wmx", "wvx", "yuv"} -- found here http://www.fileinfo.net/filetypes/video

set moviesFoldersPath to (path to application support folder from user domain as text) & "Movie Folders:"
set moviesAliasPath to path to movies folder
set posixMoviesAliasPath to POSIX path of (moviesAliasPath as text)

tell application "Finder"
	-- remove all the alias files from the movies folder
	set aliasFiles to every file of moviesAliasPath whose name extension is in movieExtensions
	repeat with anAlias in aliasFiles
		move anAlias to trash
	end repeat
	
	-- get all of the movie files from the movies folders in the application support folder
	set movieFiles to files of entire contents of folder moviesFoldersPath whose name extension is in movieExtensions
	
	-- make an alias file for each movie in the movie folder
	repeat with aMovieFile in movieFiles
		set posixMovie to POSIX path of (aMovieFile as text)
		do shell script "ln " & quoted form of posixMovie & space & quoted form of posixMoviesAliasPath
	end repeat
end tell

Bravo regulus6633,

The script works brilliantly. It’s funny that you mention linking, because I have a unix background and was about to ask how it could be implemented with apple scripts. I tried symbolic linking, but that still produced arrows, but hard links work great! I guess I never drew a distinction between the two before.

Anyways, your script works great and the removal is also working without removing the original file. No worries. Thanks alot!

Cheers,
ScKaSx

Hi regulus6633 and anyone else that might know the answer,

Actually I do have one more question. I tried to alter your script to place all the subtitle files (*.srt) in a folder called Subtitles in Movies Folder. When I run the script I get an error on the removing alias’s part. Can you check the removal portion of the script to see what I am doing wrong?

Thanks

property movieExtensions : {"3g2", "3gp", "3gp2", "3gpp", "3mm", "60d", "aep", "ajp", "amv", "asf", "asx", "avb", "avi", "avs", "bik", "bix", "box", "byu", "cvc", "dce", "dif", "dir", "divx", "dv", "dvr-ms", "dxr", "eye", "fcp", "flc", "fli", "flv", "flx", "gl", "grasp", "gvi", "gvp", "ifo", "imovieproject", "ivf", "ivs", "izz", "izzy", "lsf", "lsx", "m1v", "m2v", "m4e", "m4u", "m4v", "mjp", "mkv", "moov", "mov", "movie", "mp4", "mpe", "mpeg", "mpg", "mpv2", "msh", "mswmm", "mvb", "mvc", "nvc", "ogm", "omf", "prproj", "prx", "qt", "qtch", "rm", "rmvb", "rp", "rts", "sbk", "scm", "smil", "smv", "spl", "ssm", "svi", "swf", "swi", "tivo", "ts", "vdo", "vf", "vfw", "vid", "viewlet", "viv", "vivo", "vob", "vro", "wm", "wmd", "wmv", "wmx", "wvx", "yuv"} -- found here http://www.fileinfo.net/filetypes/video
property subExtensions : {"srt", "sub"}

set moviesFoldersPath to (path to application support folder from user domain as text) & "Movie Folders:"
set moviesAliasPath to path to movies folder
set subsAliasPath to (path to movies folder as text) & "Subtitles:"
set posixMoviesAliasPath to POSIX path of (moviesAliasPath as text)
set posixSubsAliasPath to POSIX path of (subsAliasPath as text)


tell application "Finder"
	-- remove all the alias files from the movies folder
	set aliasFiles to every file of moviesAliasPath whose name extension is in (movieExtensions or subExtensions)
	repeat with anAlias in aliasFiles
		move anAlias to trash
	end repeat
	
	-- get all of the movie files from the movies folders in the application support folder
	set movieFiles to files of entire contents of folder moviesFoldersPath whose name extension is in movieExtensions
	set subFiles to files of entire contents of folder moviesFoldersPath whose name extension is in subExtensions
	
	-- make an alias file for each movie in the movie folder
	repeat with aMovieFile in movieFiles
		set posixMovie to POSIX path of (aMovieFile as text)
		do shell script "ln " & quoted form of posixMovie & space & quoted form of posixMoviesAliasPath
	end repeat
	repeat with aSubFile in subFiles
		set posixSub to POSIX path of (aSubFile as text)
		do shell script "ln " & quoted form of posixSub & space & quoted form of posixSubsAliasPath
	end repeat
	
end tell

Actually there’s 2 errors there. First you can’t make that statement using “or” to combine the 2 lists. It just doesn’t work properly so we have to search for each kind of file separately. Second, even if your statement did work it wouldn’t find the sub files anyway. When you use the term “every file” the search will only find the files at the top level of the specified folder. If you want to recursively search through all the folders you have to use the term “files of entire contents”.

I didn’t mean you had to worry about the original movie file being deleted. I meant: suppose a user added his own movie file to his movie folder. Suppose he made a home movie using iMovie and placed that movie in his movie folder. When this script is run his iMovie-made movie will also be placed in the trash because his iMovie-made movie will have an extension that is in the movieExtensions list. Originally we told the script to only trash “alias files”, but we’re not using them any more so now we’re searching for files with the appropriate extension… which means it will trash all movie files.

property movieExtensions : {"3g2", "3gp", "3gp2", "3gpp", "3mm", "60d", "aep", "ajp", "amv", "asf", "asx", "avb", "avi", "avs", "bik", "bix", "box", "byu", "cvc", "dce", "dif", "dir", "divx", "dv", "dvr-ms", "dxr", "eye", "fcp", "flc", "fli", "flv", "flx", "gl", "grasp", "gvi", "gvp", "ifo", "imovieproject", "ivf", "ivs", "izz", "izzy", "lsf", "lsx", "m1v", "m2v", "m4e", "m4u", "m4v", "mjp", "mkv", "moov", "mov", "movie", "mp4", "mpe", "mpeg", "mpg", "mpv2", "msh", "mswmm", "mvb", "mvc", "nvc", "ogm", "omf", "prproj", "prx", "qt", "qtch", "rm", "rmvb", "rp", "rts", "sbk", "scm", "smil", "smv", "spl", "ssm", "svi", "swf", "swi", "tivo", "ts", "vdo", "vf", "vfw", "vid", "viewlet", "viv", "vivo", "vob", "vro", "wm", "wmd", "wmv", "wmx", "wvx", "yuv"} -- found here http://www.fileinfo.net/filetypes/video
property subExtensions : {"srt", "sub"}

set moviesFoldersPath to (path to application support folder from user domain as text) & "Movie Folders:"
set moviesAliasPath to path to movies folder
set subsAliasPath to (path to movies folder as text) & "Subtitles:"
set posixMoviesAliasPath to POSIX path of (moviesAliasPath as text)
set posixSubsAliasPath to POSIX path of (subsAliasPath as text)


tell application "Finder"
	-- remove all the alias files from the movies folder
	set aliasFiles to every file of moviesAliasPath whose name extension is in movieExtensions
	set subAliasFiles to every file of folder subsAliasPath whose name extension is in subExtensions
	repeat with anAlias in aliasFiles
		move anAlias to trash
	end repeat
	repeat with aSub in subAliasFiles
		move aSub to trash
	end repeat
	
	-- get all of the movie files from the movies folders in the application support folder
	set movieFiles to files of entire contents of folder moviesFoldersPath whose name extension is in movieExtensions
	set subFiles to files of entire contents of folder moviesFoldersPath whose name extension is in subExtensions
	
	-- make an alias file for each movie in the movie folder
	repeat with aMovieFile in movieFiles
		set posixMovie to POSIX path of (aMovieFile as text)
		do shell script "ln " & quoted form of posixMovie & space & quoted form of posixMoviesAliasPath
	end repeat
	repeat with aSubFile in subFiles
		set posixSub to POSIX path of (aSubFile as text)
		do shell script "ln " & quoted form of posixSub & space & quoted form of posixSubsAliasPath
	end repeat
end tell