Again, what seems like a very basic question on a topic that has me stumped. Is it possible to return just the file name (without “path:to:” part) of a file? I would like to, during the middle of a script, ensure the user that they are working with the correct file. i’ve tried the basic string manipulations (using ignoring punctuation, etc.) but that doesn’t work well. the file names are variable in length and so can’t be extracted by counting letters.
this sort of text extraction would be a very easy thing to do in perl or shell, but it would benice not to have to call one of them.
it seems like this is something that can be easily done in applescript but i haven’t found any documentation to that is terribly helpful. can anyone suggest a good book with thorough scripting language coverage (the pdf that apple gives out is helpful but limited)? then i won’t have to keep coming back with silly questions…
set name_ to name of (info for alias "path:to:file or folder")
Edit: I should note that this probably isn’t the best approach if you already have a list of file paths stored in a variable. This will actually check the file to get the name, adding overhead that isn’t necessary. One of Brandon’s suggestions is probably more efficient.
Here are a couple of ways I have gotten a filename for exactly the same purpose you are doing.
property fName: ""
set these_items to list folder aFolder without invisibles
repeat with i from 1 to the count of these_items
set f_count to count of these_items
set this_item to alias ((aFolder as text) & (item i of these_items))
set the item_info to info for this_item
if (folder of the item_info is false) and ¬
(the file type of the item_info is in the Type_List) then
set fName to name of item_info as string
display dialog fName
end if
end repeat
This one uses TID’s:
on run {}
copy (choose file with prompt "Find/Replace text in which file:") to the_file
prompt_user(the_file)
end run
on prompt_user(a_file)
copy (read a_file) to message_text
copy (text returned of (display dialog "What string would you like to replace?" default answer "Apple")) to find_me
copy (text returned of (display dialog "What string would you like to substitute?" default answer "Macintosh")) to use_me
copy (open for access file ((path to desktop as text) & "Processed Text") with write permission) to new_file_ID
write replace_chars(message_text, find_me, use_me) starting at eof to new_file_ID
close access new_file_ID
end prompt_user
on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
Apple has this as an example:
on extract_filename_from(this_filepath)
set this_filepath to this_filepath as text
set x to the offset of ":" in (the reverse of every character of this_filepath) as string
return ((characters -(x - 1) thru -1 of this_filepath) as text)
end extract_filename_from
Helpful and you’ve given me some things to play with, thanks. I’ll experiment with it.
Rob, I am familiar with the AppleScript dictionaries (at least as familiar as someone with my limited knowledge of AppleScript can be). Similar to man pages, though, i feel to a certain extent that i have to know what to look for before i can find what i want in a dictionary, and for someone new such as myself, that’s a majority of the problem. At this point i still need an intermediary guide that explains how everything fits together and i could use the dictionaries as supplemental to that. this would be especially useful for those of us that only write script sporadically and can use the guide to quickly bring ourselves back to our previous comfort level. I feel like the Scripting Language Guide offered by Apple is a good start, but just scratches the surface. I am a huge fan of the O’Reilly Nutshell books on Perl. Are you (or is anyone) familiar with the AppleScript Nutshell book and do you recommend it?
Yes, I have the nutshell book and it is a decent book but I would recommend that you get something that is more up to date. While I haven’t read either of these books, I suggest “AppleScript 1-2-3”, by Apple employee and AppleScript guru Sal Soghoian, and/or “Learning AppleScript”, by Hamish Sanderson. Both authors have a thorough understanding of AppleScript and their books are likely to be valuable resources.