Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 16, 2013, 09:56 PM   #1
AlexFW
macrumors newbie
 
Join Date: Feb 2013
Applescript error - stripping file extension

Hi, I'm using OS 10.6.8 on a Macbook Pro.

I'm trying to do something that should be really easy. All I want to do is strip the extension off a file that is sitting on the desktop and which has been selected.

This:
Code:
tell application "Finder"
	set OldName to selection
	set NewName to (characters 1 thru -4) as text
	set selection to NewName
end tell
yields this message:

Can’t make «class docf» "exp.jpg" of «class cfol» "Desktop" of «class cfol» "home" of «class cfol» "Users" of «class sdsk» of application "Finder" into type number.

I read on this forum that that kind of message usually has to do with misplaced parentheses but I tried all kinds of iterations and couldn't get it to work. But I'm not even sure the applescript itself is correct

Appreciate any help you can give,

Alex

Last edited by maflynn; Feb 17, 2013 at 12:45 PM. Reason: Code tags added
AlexFW is offline   0 Reply With Quote
Old Feb 16, 2013, 11:11 PM   #2
chown33
macrumors 603
 
Join Date: Aug 2009
My first thought was "characters 1 thru -4 of what"?

As a debugging tool, find out what each line does. Is OldName what you expect? What type is it (string, alias, text)?
chown33 is offline   0 Reply With Quote
Old Feb 16, 2013, 11:26 PM   #3
AlexFW
Thread Starter
macrumors newbie
 
Join Date: Feb 2013
Well I'm not much of an applescripter, I just pulled that "characters 1 through -4" from a whole bunch of different posts on different forums where they said that would work to strip the last 4 characters off the file name.

I did work for 4.5 hours trying to figure out what each line did before I finally gave up and decided to post here. Old name is the name of the file whose name I want to change. NewName is the name of the file without the extension, the name I want the file to be changed to. I figured it was text since it's a file name. I am not familiar with string or alias, or even that much with text.

I just figured this would be a really simple applescript to write, but I just couldn't do it. I'm just looking for suggestions from people as to why it's giving me that error and if any of my syntax is wrong.

Alex
AlexFW is offline   0 Reply With Quote
Old Feb 17, 2013, 01:00 AM   #4
Red Menace
macrumors 6502
 
Join Date: May 2011
Location: Littleton, Colorado, USA
The Finder selection is always a list, even if there is only one item, so you will need to get an item from that list (assuming that there is even anything in it) before trying to get a name from it. Also note that a name extension isn't always 3 characters, so just stripping the last 4 characters from the name might not do what you expect. Something like the following should do it:

Code:
tell application "Finder"
	set theSelection to first item of (get selection)
	set {theName, theExtension} to {name, name extension} of theSelection
end tell

if theExtension is not "" then
	set theName to text 1 thru -((count theExtension) + 2) of theName -- just the name part
	set theExtension to "." & theExtension
end if
__________________
MacBook Pro / OS X Yosemite (10.10) / Xcode 6.1 / RubyMotion FTW
Red Menace is offline   0 Reply With Quote
Old Feb 17, 2013, 10:06 AM   #5
AlexFW
Thread Starter
macrumors newbie
 
Join Date: Feb 2013
Hi Red, thank you for your reply and script! Unfortunately it didn't work. No idea why.

I am aware that the name extension isn't always 3 characters. I was just hoping to come up with something for the vast majority of the time. Most of the extensions I want to strip are jpg & pdf. I know the script the way I had written it would not strip all of, for example, tiff. I appreciate your thinking of that contingency though.

I don't know why the script you wrote isn't working for me. I select the object on the desktop. Then I run the script. Nothing happens. Can you help further?

Thanks
AlexFW is offline   0 Reply With Quote
Old Feb 17, 2013, 12:07 PM   #6
Partron22
macrumors 6502a
 
Join Date: Apr 2011
Coercion

This'll get you part way to where you want to go:
Code:
tell application "Finder"
	set OldName to selection as alias
	set NewName to (characters 1 thru -4 of (OldName as text)) as text
	set name of file OldName to NewName
end tell
You'll still need to strip off a lot of the path info.
Look into Applescript's "set text item delimiters."
Partron22 is offline   0 Reply With Quote
Old Feb 17, 2013, 12:29 PM   #7
Red Menace
macrumors 6502
 
Join Date: May 2011
Location: Littleton, Colorado, USA
Quote:
I don't know why the script you wrote isn't working for me. I select the object on the desktop. Then I run the script. Nothing happens. Can you help further?
Works just fine for me - check the event log to see if the Finder is getting the selection (it can be a bit sporadic in Lion/Mountain Lion). The script just gets the name and extension parts of the file name, so it is up to you to do whatever you are going to do with them, for example:

Code:
display dialog theName & return & theExtension
__________________
MacBook Pro / OS X Yosemite (10.10) / Xcode 6.1 / RubyMotion FTW
Red Menace is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
System Extension Error ?? Washac Mac Pro 2 Apr 27, 2014 09:35 AM
Missing file extension spb3 Mac Basics and Help 4 Apr 3, 2014 12:30 AM
USB Mount Extension Error Slrunna OS X Mavericks (10.9) 0 Dec 10, 2013 03:51 PM
dce extension error? jgmacrum OS X 2 May 29, 2013 03:21 PM
Safari 6.0 extension error message rainbowizard Mac Applications and Mac App Store 1 Jul 29, 2012 10:56 PM

Forum Jump

All times are GMT -5. The time now is 04:30 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC