Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

AlexFW

macrumors newbie
Original poster
Feb 16, 2013
5
0
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 a moderator:
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)?
 
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
 
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
 
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
 
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."
 
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
 
The shell command 'basename' will strip an extension and the leading directory path, leaving just a base name. The 'dirname' command will return just the directory path.

So maybe use AppleScript's "do shell script" with those commands, and the 'Posix path' property of an alias.


We might be able to make other suggestions if we knew what the goal is. Stripping .pdf and .jpg is only one part. What's the big picture?
 
Hi everybody and thanks. To answer some of your questions:

My big picture: It is so simple that it seems overlooked. I just want to be able to remove the extension from a file name. That is it. I will then hook the applescript up to a keyboard command. So that I can, with one keystroke, get rid of an extension on a file. I find extensions really distracting. There is no setting to choose "hide extensions" as default (no, the system preferences setting for this does not work to globally hide extensions on all files on the computer and coming onto the computer).

So for example, the file "exp.jpg" is renamed to "exp".

Event log: The event log is returning "jpg". However, the script is still not changing the name of the file. (I am running Snow Leopard.) The same is true for Partron's script. I would also not know how to remove the path, I did try to study text delimiters but got completely lost by that.

So I guess what I'm saying is, what command will actually rename the file? To the name of the file without the extension? As I said before, I am not much of an Applescripter so all the help you can give and are giving is appreciated.

Do Shell Script: Any way you could write it out for me? I just tried to look it up and I can't get clear on how it works.
 
My big picture: It is so simple that it seems overlooked. I just want to be able to remove the extension from a file name. That is it. I will then hook the applescript up to a keyboard command. So that I can, with one keystroke, get rid of an extension on a file. I find extensions really distracting. There is no setting to choose "hide extensions" as default (no, the system preferences setting for this does not work to globally hide extensions on all files on the computer and coming onto the computer).

So for example, the file "exp.jpg" is renamed to "exp"..

It's a bad idea to remove the extension of the actual filename. The Finder may become confused about which program to open it in. Other programs may also fail to recognize the file as openable.

A better approach is to hide the file extension of the file, on a per-file basis.


First, I googled finder hide file extensions and found various articles describing different solutions. It also described the general situation, which I was not that familiar with. I always run with the exact opposite: I always want to see the file extension, even if the file has the per-file hide-extension flag set.

The google results led to different AppleScripts to accomplish this, and they all are similar to the code shown below. For example, here's one that's a droplet (you drop files on it):
https://forums.macrumors.com/posts/16816809/

Here's another one that's a Hazel script:
http://apple.stackexchange.com/questions/9700/is-there-a-way-to-hide-all-file-extensions-in-os-x

Also see this article:
http://support.apple.com/kb/PH10845

And see this discussion for reasons why hidden extensions pose a security risk:
https://discussions.apple.com/thread/2301219?start=0&tstart=0


In an Automator workflow, the output from the previous action is passed as an input list to an AppleScript action's run handler:
Code:
on run {inputList, parameters}
	repeat with each_item in inputList
		tell application "Finder"
			set extension hidden of each_item to true
		end tell
	end repeat
	
	return inputList -- for any subsequent action
end run
So start with a Service template in Automator, add a Run AppleScript action, then paste in the code. Set the "Service receives" to "files or folders" in "any application". Save, then test it (once saved, it should appear in Finder's contextual menu).

For testing, you can also use a Get Specified Finder Items action. Disable it after testing.

To see the extensions hidden, the Finder Preference "Show all filename extensions" must be UNchecked.

EDIT

Also see this recent post.
 
Last edited:
Hi chown33, Thank you very much for your posting. On the confusing-the-finder and other programs issue, I've been using Macs since 1985 and hand-removing extensions, and have never had any trouble with the finder not knowing what ap to open the file with. After removal of the extension, the icon retains the application icon it's tied to. As far as the Trojan, I've never any trouble with that either, and I'm willing to take the risk.

I already have an applet that will hide the extension of any file dropped onto it. However, the applescript that the applet is made from does not seem to change the name of the selected file. That's why I tried to figure out how to do it. I don't want to have to use an applet. I want to be able to select the item and use a key command to eliminate the extension.

I have a unique reason for doing what I want to do. I have some cognitive deficits which affect the way I visually process file names. I am able to function much better and my work is smoother when the extensions are hidden.

I thought this was going to be a pretty easy matter, but I guess not. Again, I am NOT a very advanced applescripter. I do not understand Hazel scripts, aliases, Posix paths or any of these specialized terms. Much of the suggestions that have been posted here, although appreciated, are way over my head. I am not stupid, I am just not an applescripter beyond some very basic scripting I have done here and there. Having put a number of hours into this already, I felt that rather than having to study and learn a bunch of applescript concepts, that I could just ask here on the forum for a sample script that works.

1) I tried pasting the text of the "Hazel script" in the one link you gave me, into a script editor, selecting my file, and running it. I got this message:

tell application "Finder"
set extension hidden of item 1 to true
end tell

The script runs, the event log says "true", but the file extension does not disappear.

2) I tried running the script in the link you said was an applet on my file, and I got the message "The variable posixPath is not defined."

3) The link to the article you provided about the Finder preference to hide all extensions is something I am very familiar with. But as I said before, it does not work. Never has, as long as I've been using Macs. This has been confirmed by many other forum posts. It's some Apple quirk. Maybe they finally fixed it in Mountain Lion, but I'm on Snow Leopard and they still haven't fixed it in Snow Leopard.

4) Prior to posting my original post here, I had already come across and reviewed the article about hiding extensions being a security risk. In that article, I agree with the person who has not found that to be the case with later systems and opined that perhaps that was a problem with earlier systems. That article also refers to computers networked to servers, which mine is not.

5) Unfortunately I am not familiar with Automator or workflows, so I would not be able to implement your suggested script using the same. I did try just pasting it into a script editor and running it and I got an error.

6) Finally, I am familiar with the command-option-I ("get info) function where you check the box to hide the extension. This takes just as long as manually removing the extension. I was hoping for a key command to do the same.

Once again, I very much solicit your continued assistance until I can get this solved.

Thank you!
 
This runs on Lion. Let me know if it throws errors in Snow Leopard and I'll tweak as needed.

Code:
tell application "Finder"
	set aliasSelectedFile to selection as alias
	set textExtension to name extension of aliasSelectedFile
	
	if textExtension is not "" then
		set textFileNameSansExt to text 1 thru -2 of (do shell script "basename" & space & quoted form of POSIX path of aliasSelectedFile & space & textExtension)
	end if
	
	set name of aliasSelectedFile to textFileNameSansExt
end tell
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.