I need to display the name of a file in a studio app.

hello! for an applescript studio app I’m creating I need to display the name of a file. I have the Path to the file correctly, but I just want to display the name and for the life of me I can’t figure out how. I keep getting errors regarding not being able to reference the alias or something of the sort.

Also, I’d like to set the items of a popup button to the names of folders in a certain location. How would I go about doing that?

Thanks!!

-JF

Getting the name of the file should be pretty simple. if you have the path stored in a variable called thePath then:


set theName to the name of item thePath

That’s it. As far as setting the contents of a popup button, if the popup button is named “Folder Popup” then you’d want to do someting like this:


tell window 1
delete every menu item of menu of popup button "Folder Popup"
repeat with i from 1 to count of folders of the desktop
set theName to the name of folde i of the desktop
make new menu item at the end of menu items of menu of popup button ¬
     "Folder Popup" with properties {title:theName, enabled:true}
end repeat
end tell

The “delete” at the beginning is just housekeeping to clear the menu before you start adding items to it just in case. “dekstop” can be replaced by the path to the folders you want in your popup. Note that the line beginning “make new menu item…” ends in a continuation character (¬). This means that the following line is just a continuation of this line and should be thought of (and typed) as one line. Script Editor understands and uses this continuation character to help you keep your script fitting inside your window. If you cut and paste, you’re going to want to get rid of the spaces before “Folder Popup”.

Hope this helps.

jakacmar:

Thanks for the swift reply, but I’m still having trouble with this - and I feel like a complete moron! ha!

here’s the code I’m using:


		set thePath to choose file with prompt "Choose Scene File:"
		
		set theName to the name of item thePath


when I execute that in my app, I get the error “NSCannotCreateScriptCommandError(10)”

all I want to do is display the name of the file selected in a field so the user knows what they’re working with… and I’m clearly missing something.

any thoughts?

I have not tried the folder list yet…

Does this work?

set thePath to (choose file with prompt "Choose Scene File:") as Unicode text

set tids to text item delimiters
try
	set text item delimiters to ":"
	set theName to thePath's last text item
	set text item delimiters to tids
on error
	set text item delimiters to tids
end try

display dialog theName

– Rob

Are you sure that it’s this part of the script that is causing the error? Testing this in Script Editor using the Finder, it works as expected. e.g.,

tell application “Finder”
set thePath to (choose file with prompt “Choose Scene File:”)
set theName to the name of item thePath
display dialog theName
end tell

Will display a dialog with the name of the file. Perhaps there’s a mistake in passing this info to your app for display. It should definitely work since “set theName” should return a string of text. But then again, if your AS Studio apps are as big of a messs as mine usually turn out to be, any number of things could be causing the problem, and troubleshooting AS apps in XCode is a joke so I usually end up using trial and error. You may want to try having your app “display dialog theName” instead of passing it on to the rest of your app to see if this is what is really giving you the problem. If the dialog pops up correctly with the correct name, then you know the error is somewhere else.