Ambrosia Software Web Board: "Create File" Applet - Ambrosia Software Web Board

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

"Create File" Applet For your enjoyment

#1 User is offline   PCheese 

  • Member
  • PipPipPip
  • Group: Members
  • Posts: 485
  • Joined: 04-June 01

Posted 24 February 2005 - 08:39 PM

After reading this hint on macosxhints.com, I decided to write my own script with additional features. Everything was working fine until I decided to let the user add his or her own file/creator types through AppleScript dialog boxes. The script maintains a list of possible file/creator type combinations and lets the user select one of them; adding a custom file/creator type creates another entry at the end of the record. However... these custom entries give me errors for no discernible reason. Here's the event log for a standard file/creator type:
tell application "Finder"
	set file type of file "HD:Users:me:New Text Document.txt" to "TEXT"
  "TEXT"
	set creator type of file "HD:Users:me:New Text Document.txt" to "ttxt"
  "ttxt"
end tell

And here's the event log for a custom one:
tell application "Finder"
	set file type of file "HD:Users:me:New Text Document.txt" to "TEXT"
end tell
tell current application
	display dialog "Finder got an error: Can't make some data into the expected type."
  {button returned:"OK"}
end tell


Both tell the Finder to set the file type of a file to the same string, yet one causes an error while the other works fine.

Ahh it's driving me crazy, can someone explain why or how to fix this? I tried everything I could, including pulling out my trust 400-page AppleScript Language Guide to see if there were any classes other than string or text that might work...

[edit]oops wrong second link[/edit]

This post has been edited by PCheese: 25 February 2005 - 07:43 PM


#2 User is offline   Lankhmart 

  • Junior Member
  • Group: Members
  • Posts: 10
  • Joined: 22-April 03

Posted 25 February 2005 - 03:52 AM

Both of your links point to the same script with a hard-coded list of file types (and it works fine for me), but I'm guessing that what's failing you is using the text returned from a display dialog command. Something like:

set creatorType to text returned of (display dialog "Enter creator code:" default answer "ttxt")
tell application "Finder" to set creator type of someFile to creatorType


The problem is that display dialog returns Unicode text (as of 10.3), while the Finder expects a plain string, and a simple "as string" coercion (or "as text") doesn't actually convert the Unicode into plain text. Instead we end up with styled text (I'm not really sure why), even though AppleScript will tell you that the class of the object is simply string.

set creatorType to text returned of (display dialog "Enter creator code:" default answer "ttxt")
set creatorType to creatorType as string
log (class of creatorType) -- *string* is logged
tell application "Finder"
    set creator type of someFile to creatorType
    --error: can't make some data into the expected type
end tell


So the trick is to strip away the style data and pull out just the bare string. Here is one way to go about it:

set creatorType to text returned of (display dialog "Enter creator code:" default answer "ttxt")
set creatorType to «class ktxt» of (creatorType as record)
tell application "Finder"
    set creator type of someFile to creatorType
end tell


A bit uglier than it ought to be, but it does the job.

#3 User is offline   PCheese 

  • Member
  • PipPipPip
  • Group: Members
  • Posts: 485
  • Joined: 04-June 01

Posted 25 February 2005 - 11:42 AM

Ugly? Haha, it's beautiful (because it works)! Thank you very much.

Go get it!

This post has been edited by PCheese: 25 February 2005 - 04:59 PM


#4 User is offline   Lankhmart 

  • Junior Member
  • Group: Members
  • Posts: 10
  • Joined: 22-April 03

Posted 25 February 2005 - 01:19 PM

PCheese, on Feb 25 2005, 04:42 PM, said:

Ugly?  Haha, it's beautiful (because it works)!  Thank you very much.


You're welcome. I'm glad to hear that it's working.

As a side note (and to anyone who is having trouble compiling the script as-is): The web server is not specifying a text encoding for the script file, so non-ASCII characters like the "¬" continuation character and the chevrons (« and ») around "class ktxt" are not being represented properly by default. You can tell your browser to use an alternate encoding, though. In Safari, select View -> Text Encoding -> Western (Mac OS Roman) -- or use a similar procedure in Firefox, Mozilla, etc. Now the text should display correctly and you can paste into Script Editor without a problem.

#5 User is offline   PCheese 

  • Member
  • PipPipPip
  • Group: Members
  • Posts: 485
  • Joined: 04-June 01

Posted 25 February 2005 - 05:03 PM

Lankhmart, on Feb 25 2005, 01:19 PM, said:

The web server is not specifying a text encoding for the script file...
View Post


Oops. I made some changes to the code, and it's now available precompiled in a .zip archive with a custom icon that'll fit in nicely with your Finder toolbar :P : Create File AppleScript

#6 User is offline   Codeglove 

  • Dua
  • PipPip
  • Group: Members
  • Posts: 341
  • Joined: 06-October 04
  • Location:In front of my keyboard.

Posted 26 February 2005 - 04:09 AM

I've been wanting a contextual menu plugin for this since... I dunno. I saw that in (atleast some version of) Windoze you have "create new <file type>" menu items in the right-click menu in it's version of the Finder.

So when I saw this topic I thought I'd just simply do it using a plugin. It turned out that just getting the contextual menu to show up in the finder was hard enough, so I gave up. :P I'd still like to do it, though...

'glove
Compilers - the ultimate god games.
I take no responsibility of any kind for any damage, hurt feelings or other negative things caused, directly, indirectly or otherwise, by the information, opinions or lack of either in this post, or any other post on this board or any other board.

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users