Hi, I’m currently trying to find a way to convert a text string to a property or a text constant.
More specifically, I’m writing a Finder’s window’s current view to a plist and later in a separate script getting that information, which comes back as a string (not as the text constant it once was).
set theView to “list view” --text string that comes back from plist
set theView to (run script theView) --unfortunately this only works with integers
I’ve searched around a lot and have not found any good information about the subject yet. I’ve thought about maybe using a script library which uses the framework “Foundation” but I haven’t found how to actually to the conversion.
Surely there’s a way to convert a string to a text constant, no?
There’s no conversion available – you need to do it something like this:
if theView is "list view" then
set theView to list view
tell application "Finder" to set theView to list view
This seems to work for Finder enums that are property values or class names. It doesn’t work for property labels unless they happen to be the same as a class name (eg. list view options — and then presumably it’s the class token that’s returned, not the label token) because the run script will then try to return the properties’ values instead.
set theView to "list view"
set theView to (run script "tell app \"Finder\" to return " & theView)
Ah. This is definitely what I’m looking for, Nigel. I’d just hate to set up a bunch of ifs and variables because it all feels and looks too bulky and when they update the OS and add another one I have to remember to go in to add that new Finder window view to my script. I’m hoping this will provide some type of future-proofing that will save time in the long run.
An AppleScript constant is made up of two four-byte code sequences, the latter of which uniquely defines the constant, whilst the former groups constants into related family members.
Here are some constants together with their raw syntax code forms:[format]http URL «constant eschhttp» downloads folder «constant afdrdown»
mail URL «constant eschmail» documents folder «constant afdrdocs»
news URL «constant eschnews» applications folder «constant afdrapps»[/format]
The last four characters of each eight-character code is what you’ll be interested in for your task. For the documents folder constant, this four-character code is “docs”. So, if you run this line of AppleScript in Script Editor:
"docs" as constant
you’ll get back documents folder in its familiar form as an AppleScript constant. To go the other way:
documents folder as «class utf8»
which yields “docs”
Here are two basic handlers that leverage this principle:
-- Converts a four-character code into
-- the equivalent AppleScript constant
on enum(str as «class utf8»)
str as constant
-- Converts an AppleScript constant into
-- a four-character code sequence
on str(enum as constant)
enum as «class utf8»
You may choose to add in some error handling, because if you pass enum() anything that isn’t or doesn’t immediately convert to a string containing exactly four characters (which can be any four unicode characters), it will throw an error.
For your particular use case:
use application id "com.apple.finder"
str(list view) --> "lsvw"
enum("lsvw") --> list view