A few days ago, I posted the script included below in the Mac OS X forum. The script works as expected as long as the value of theFileName variable fits the specified dot pattern; when it doesn’t an NSAppleEventDescriptor is returned. I spent some time with Google and the documentation but this is beyond my current level of understanding. How do I get the script to return an error in a usable format when the script is unable to work correctly. Thanks for the help.
The script is:
use framework "Foundation"
use scripting additions
set theFileName to "tv.show.title.S01e02.misc.garbage.characters.mkv" --> works OK
set theFileName to "aa.bb" --> returns NSAppleEventDescriptor
set newFileName to getFileName(theFileName)
on getFileName(theString)
set theString to current application's NSString's stringWithString:theString
set arrayOne to (theString's componentsSeparatedByString:".")
set arrayTwo to arrayOne's valueForKey:"capitalizedString"
return (current application's NSString's stringWithFormat_("%@ %@ %@ %@.%@", item 1 of arrayTwo, item 2 of arrayTwo, item 3 of arrayTwo, item 4 of arrayTwo, item -1 of arrayOne) as text)
end getFileName
Just a guess here, but it could be because your accessing a number of elements in the array that don’t exist.
In this line here, your trying to access five elements in the array.
return (current application's NSString's stringWithFormat_("%@ %@ %@ %@.%@", item 1 of arrayTwo, item 2 of arrayTwo, item 3 of arrayTwo, item 4 of arrayTwo, item -1 of arrayOne) as text)
But this line here will only give you two elements, when converted into an array separated by “.” character.
set theFileName to "aa.bb" --> returns NSAppleEventDescriptor
Maybe check the number of elements first, before trying to access them.
Mark is right, the array must contain at least 5 items to work correctly.
This version counts the number of items. The last object is the file extension and if there are more than 5 items only the first 5 items are considered
on getFileName(theString)
set theString to current application's NSString's stringWithString:theString
set arrayOne to (theString's componentsSeparatedByString:".")
set arrayTwo to arrayOne's valueForKey:"capitalizedString"
set fileExtension to arrayOne's lastObject() as text
set arrayLength to arrayTwo's |count|() as integer
if arrayLength > 4 then
set maxLength to 4
else
set maxLength to arrayLength - 1
end if
set indexSet to current application's NSIndexSet's indexSetWithIndexesInRange:{0, maxLength}
set subArray to arrayTwo's objectsAtIndexes:indexSet
return ((subArray's componentsJoinedByString:" ") as text) & "." & fileExtension
end getFileName
Thanks Mark, Stefan, and Fredrik71 for the great ideas. I had my mind fixed on getting an error result from NSAppleEventDescriptor and completely missed simple and effective ways of doing what I wanted.
Thanks technomorph for responding to my post. I have modified my original script to delete “item x of” and to use objectAtIndex instead.
Just as an aside, it’s probably not technically correct but “item x of” does seem to work:
use framework "Foundation"
set theArray to current application's NSArray's arrayWithArray:{"a", "b"}
item 1 of theArray --> (NSString) "a"
The following is from Shane’s book and–while the situations are not directly analogous–the thought occurred to me that perhaps “item x of” works with an array for a similar reason that the AppleScript count command works with an array.
I ran a quick timing test with Script Debugger, and “item x of” is marginally faster than objectAtIndex in my script. The concern remains, however, that the use of “item x of” may cause the script to break at some future point.
I also may have happened upon a possible answer as to why the script returns an NSAppleEventDescriptor. The following is from Shane’s book:
My guess is that the array is converted “under the hood” to a list, but items 3 and 4 of the newly-created list cannot be converted to an NSString. So these items are instead stored as NSAppleEventDescriptors.