How to implement strncpy() functionality ?

Hi all,

Sorry if I ask the fundamental question, I’m new with Applescript.

I have a path of a document contains extension, how can I cut off the extension or generally modify the string as we do with strncpy() ? I need to be able to cut from nth char to (n+x)th char of string to make a new string.

Regards, Nima

This on simply divides the text into two list items (the file name and the extension) and returns the first item only.

Any string with dots in will be split into items.


set mystring to "myfile.txt"

set mystring to my strip_ext(mystring)

display dialog mystring

on strip_ext(mystring)
	set AppleScript's text item delimiters to "."
	set the item_list to every text item of mystring
	return the first item of item_list
end strip_ext

strncpy() works with bytes. In AS, when using strings, you can refer to characters like…

set x to characters 1 through 7 of y

Or you can count from the right with -. Saves you the trouble of subtracting from length.

set x to characters 2 through -1 of y (The -1 means ‘next to last’)

you can refer to other collections too, like words.

markjeater gave you some good sample code for stripping the extension, but it would break on mark.jeater.text.

items 1 through -1 would be what you want. First item would only return ‘mark’

I like this approach, where choose file is substituted for an alias to your file (I’m assuming you want just the name and not the rest of the path without the extension):


tell (info for (choose file)) to set basename to name's text 1 thru ((my (offset of ("." & name extension) in name)) - 1)