Making and moving aliases and duplicates of files

Hey gang

Its me again, asking for some help :stuck_out_tongue: I’ve got most of the kinks worked out of my main script, but i’m working on a few secondary scripts, to help the user.

My first is an import script, so that the user can target the script that they want to use, and import it into the scripts folder. I would like 3 ways of doing this, which I have, but only 1 works. Those 3 ways are Alias, Copy, and Move. Alias, due to the fact that by doing that, entire games can be imported into the folder and run using this script. (I.E- make an alias of “Medal of Honor” for example, and that alias can be launched from the script). Copy, in case the script in question happens to be a bundle, in which case an alias won’t work, or Move, which just moves the script itself into the folder.

As of now, only move works.
Alias returns an error saying that you can only make aliases of folders and disks, so i’m thinking I havn’t got the correct syntax there.
Copy correctly duplicates the file, but fails to move it into the folder. It also, as a side effect, names the resulting file wish a “copy” at the end. I can think of one way that could be solved, but its not ‘optimal’ so if any of you know how to change its name back to its orginal name after its imported, that would be much appreciated.

Now then, my script (I will come up with some more creative naming schemes before I release this thing :P)

--path to Myscripts
set mypath to (path to me)
tell application "Finder"
	set Myscripts to (container of mypath) as string
	set scriptsfolder to (Myscripts & "Myscripts") as alias
end tell
--main script
tell application "Finder"
	display dialog "How do you wish to import your Script or Application?" buttons {"Alias", "Copy", "Move"} default button 2
	if button returned of the result is "Alias" then
		set thescript to choose file with prompt "Select yourscript"
		set thealias to (make alias of thescript)
		move thealias to folder scriptsfolder
	else
		if button returned of the result is "Copy" then
			set thescript to choose file with prompt "Select your script"
			set thecopy to (make (duplicate of thescript))
			move thecopy to folder scriptsfolder
		else
			if button returned of the result is "move" then
				set thescript to choose file with prompt "Select your script"
				move thescript to folder scriptsfolder
			end if
		end if
	end if
end tell

The other question, unrelated to this script, is how to check a folder to be sure it doesn’t contain any disallowed filetypes. (I.E- make sure that only .scrpt or .app files are in the folder, not .png, or .gif, or .dmg or whatever else)

Thanks again :smiley:

Is this the sort of thing you wanted to achieve with your Move / Duplicate / Alias commands?

set MyFile to choose file with prompt "Choose file to copy"
set MyFolder to choose folder with prompt "Choose destination folder for this file"
set CopyMode to button returned of (display dialog "Choose copy mode" buttons {"Duplicate", "Alias", "Move"} default button "Duplicate")
CopyFile(CopyMode, MyFile, MyFolder)

on CopyFile(CopyMode, MyFile, MyFolder)
	tell application "Finder"
		if CopyMode = "Duplicate" then
			duplicate MyFile to MyFolder
		else if CopyMode = "Alias" then
			make alias to MyFile at MyFolder
		else if CopyMode = "Move" then
			move MyFile to MyFolder
		end if
	end tell
end CopyFile

I think the problem you have with Finder appending “Copy” to your duplicates was that you had duplicated it to the original location before moving it, which would have caused a name clash. It’s easier just to duplicate the file straight to the target destination.

As for ensuring that only certain file types / extensions are present, how about the following (it sorts the files of the given folder into 2 sublists: allowed and disallowed files):

set MyFolder to choose folder with prompt "Choose folder to examine for file extensions"
tell application "Finder"
	set AllFiles to every file in MyFolder
	set AllowedExtensions to {"scpt", "app"} -- Add whatever extensions you wish to allow
	set Allowed to {}
	set Disallowed to {}
	repeat with i from 1 to count of AllFiles
		if AllowedExtensions contains name extension of item i of AllFiles then
			copy item i of AllFiles to end of Allowed
		else
			copy item i of AllFiles to end of Disallowed
		end if
	end repeat
end tell
return {Allowed, Disallowed}