Return NSArray of NSURLs filenames without extensions

I have handlers that produce NSArrays of NSURLs. I have other handlers that will return an NSArray of the paths, the names etc from these NSArrays of NSURLs.

I’m trying to implement the same type of handlers for the file extension and the base name.

on FileSystem_NSURLs_Names(NSURLArray)
	try
		set NSURLArray to (current application's NSMutableArray's arrayWithArray:(NSURLArray))
		considering numeric strings
			return ((((NSURLArray's valueForKey:"lastPathComponent")'s componentsJoinedByString:(character id 0))'s stringByReplacingOccurrencesOfString:":" withString:"/")'s componentsSeparatedByString:(character id 0)) as list
		end considering
	on error errorText number errornumber partial result errorResults from errorObject to errorExpectedType
		error "<FileSystem_NSURLs_Names>" & space & errorText number errornumber partial result errorResults from errorObject to errorExpectedType
	end try
end FileSystem_NSURLs_Names

Returns the filenames ( with extensions ) of the NSURLs.

on FileSystem_NSURLs_File_Etensions(NSURLArray)
	try
		set NSURLArray to (current application's NSMutableArray's arrayWithArray:(NSURLArray))
		return (NSURLArray's valueForKey:"pathExtension")
	on error errorText number errornumber partial result errorResults from errorObject to errorExpectedType
		error "< FileSystem_NSURLs_File_Etensions >" & space & errorText number errornumber partial result errorResults from errorObject to errorExpectedType
	end try
end FileSystem_NSURLs_File_Etensions

Returns an array of NSString file extensions.

How can I similarly, i.e. without a repeat loop, return an NSArray of the NSURLs file names without their extensions?

You have to call valueForKey twice: First with key URLByDeletingPathExtension and then with key lastPathComponent

on FileSystem_NSURLs_BaseNames(NSURLArray)
	try
		set NSURLArray to (current application's NSMutableArray's arrayWithArray:(NSURLArray))
		considering numeric strings
			return (((((NSURLArray's valueForKey:"URLByDeletingPathExtension")'s valueForKey:"lastPathComponent")'s componentsJoinedByString:(character id 0))'s stringByReplacingOccurrencesOfString:":" withString:"/")'s componentsSeparatedByString:(character id 0)) as list
		end considering
	on error errorText number errornumber partial result errorResults from errorObject to errorExpectedType
		error "<FileSystem_NSURLs_Names>" & space & errorText number errornumber partial result errorResults from errorObject to errorExpectedType
	end try
end FileSystem_NSURLs_BaseNames

1 Like

Thanks StefanK! Hopefully your solution also helps me wrap my head around this usage. We shall see.

Hi.

An alternative would be to use valueForKeyPath:

on FileSystem_NSURLs_BaseNames(NSURLArray)
	try
		set NSURLArray to (current application's NSMutableArray's arrayWithArray:(NSURLArray))
		return ((((NSURLArray's valueForKeyPath:"URLByDeletingPathExtension.lastPathComponent")'s componentsJoinedByString:(character id 0))'s stringByReplacingOccurrencesOfString:":" withString:"/")'s componentsSeparatedByString:(character id 0)) as list
		-- Or:
		return ((((NSURLArray's valueForKeyPath:"lastPathComponent.stringByDeletingPathExtension")'s componentsJoinedByString:(character id 0))'s stringByReplacingOccurrencesOfString:":" withString:"/")'s componentsSeparatedByString:(character id 0)) as list
	on error errorText number errornumber partial result errorResults from errorObject to errorExpectedType
		error "<FileSystem_NSURLs_Names>" & space & errorText number errornumber partial result errorResults from errorObject to errorExpectedType
	end try
end FileSystem_NSURLs_BaseNames
4 Likes

@Nigel_Garvey this is easier for me to understand. Thanks!