Get Name Of file with Drag and Drop on the icon

So here is my problem.

With a drag and drop application, i want to get the name & name of extension of the drop file.

	on application_openFiles_(theApp, fileList)
		repeat with theFile in fileList
			tell application "Finder"
				set mySelect to (theFile as text) as POSIX file
				set nameFile to name of mySelect
			end tell
		end repeat
	end application_openFiles_

i nearly sure that i’m not very far from succes, but i have the error :
“Finder got an error: Can’t get name of file “MacPro:Users:.:desktop:fichier ABC.pdf”. (error -1728)”

So what’s wrong ?

I think you can do this more simply without using the finder. Since fileList contains the path’s of all the files, all you need to do is get the last path component:

on application_openFiles_(theApp, fileList)
		repeat with theFile in fileList
			set nameFile to theFile's lastPathComponent()
		end repeat
	end application_openFiles_


Thanks ! It works fine.
So i also found how to get the extension :

			if theFile's pathExtension() as string is "pdf" then
				display dialog "c'est unPDF"
			end if

But now i tried to change the name of file with “set” like that

			theFile's setLastPathComponent_("newname" as string) 

but it give the error : “[NSCFString setLastPathComponent:]: unrecognized selector sent to instance 0x20034e140 (error -10000)”

My question is how to set a new name ?

You should look at the “Working With Paths” section of the NSString Class Reference, all the methods you need are there. You’ll need to take off the old last path component first, and then add the new one.



I find on the developer site:

how to delete
“- (NSString *)stringByDeletingLastPathComponent”

how to add name
“(NSString *)stringByAppendingPathComponent:(NSString *)aString”

but it is in Cocoa not ASOC
so i don’t know how to use them.

If your level of experience with ASOC is such that you can’t use these methods, then I suggest that you buy a copy of Shane’s book – it’s a good introduction to ASOC and includes many projects with code.

I will give a very brief explanation here of how to use these methods. Notice that both methods start with a “-” which means that they are instance methods (a + means class methods). This means that you send the message to an instance of an NSString, like theFile in your example. Also, for methods that have no colon in objective C (meaning that they take no arguments) you have to put () at the end of the method name in ASOC, so to use stringByDeletingLastPathComponent, you would do:

set fileWithoutName to theFile’s stringByDeletingLastPathComponent()

stringByAppendingPathComponent: has a colon at the end, which in ASOC you convert to an underscore followed by the parameter in parentheses. The signature for the method, -(NSString *)stringByAppendingPathComponent:(NSString *)aString tells you that the method takes a string as the argument and returns a string as the result. So, to use this method you would do the following:

set newFile to fileWithoutName’s stringByAppendingString_(“newName.pdf”)

You don’t need to add “as string” to the end, cocoa methods, when used in ASOC can accept applescript strings as input.


I already buy it. I agree with you it’s a very very good book (PDF) !! :smiley:

But i never understand how to translate from cocoa to ASOC. (may be because my english is not good enough to understand everything).

So i thank you for this explanation. It will help me a lot.

PS : the small program i’m doing is for me. I’m not a programmer. I enjoy to do it (being crazy when it doesn’t work) but i’m not a professional (only amateur)

Thanks again for helping everyone in this forum