Hi
I start learning AppleScript, wrote small script operating on TextEdit and don’t understand why after last lien I see error "Invalid object reference.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "TextEdit"
activate
set doc to make new document with properties {name:"Info", text:""}
set name of doc to "Hello"
set text of doc to "ABC"
end tell
It looks like set name of doc to “ABC”
overrides doc property with wrong value.
I’ve tried to find explanation, bought 2 books about AppleScript but still don’t understand (basic??) rules .
For C/Python/Rexx programmer AppleScript seems to be very strange language.
I think the issue us that you change the name of the document to “Hello” and you then try to write to a doc named “Info”. The following works on my Sequoia computer:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "TextEdit"
activate
set doc to make new document with properties {name:"Info", text:""}
set text of doc to "ABC"
set name of doc to "Hello"
end tell
Why. create a new doc then change its name. Create the new doc with the correct name in the first place. Like so…
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "TextEdit"
activate
set doc to make new document with properties {name:"Hello", text:""}
set text of doc to "ABC"
end tell
OK thanks
But for me that is strange situation and I try to understand the logic.
If I have objet with property representing name and want to change their name - I just set new value of property (at least in other languages or object environments). Here it seems that old object disappears.
Strange, very strange. The original file was without the name and later I’ve tried to change the name to concrete value ant it also got the same error.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "TextEdit"
activate
set doc to make new document
set name of doc to "Hello"
set text of doc to "ABC"
set text of doc to "UVW"
end tell
When you create a document in TextEdit, the specifier you get back (ie. the value assigned to your doc variable) is a name reference, such as document “Info” of application “TextEdit”, or document “Untitled 2” of application “TextEdit”. When you change the name of the document, it doesn’t change the value of doc, so doc now refers to a document with a name that no longer exists.
Confusingly (at first), applications written by different programmers may return different kinds of reference, such as document 1 of application “Blah” or document id 1234 of application “blah”.
I suggest upgrading from Script Editor to Script Debugger (which is now free). Using its variable viewing and application exploring features are the best way to learn AppleScript.
I have ScriptDebugger (bought it above 1 year ago) and I in this case I’ve seen that reference “disappeared” because I observed variables in inspection window.
But trust me - it is not easy for user with 30 years of practice in Unix tools to start programming (and thinking) in AppleScript
BTW during tests Script Debugger was hanging a few times
The last version ever published of the AppleScript Language Guide is on line here. The section on object specifiers is here and the chapter on reference forms here. Otherwise, because AppleScript’s a scripting language designed to communicate with software written by third-party developers who may have their own ideas about things, it’s always been a language where you learn the basics, then just experiment to see what works with the software you want to control!
The problem here is that you’re expecting a durable object reference to the document you created. Text Edit is a tiny, old, weak text editor written in 1996 by NEXT and it doesn’t provide one it only provides a reference to the properties of the document… None of the properties are persistent like an ID etc.
You could resolve this by reconstructing your reference at each crucial modification…
tell application "TextEdit"
activate
set doc to make new document with properties {name:"Info", text:""}
set name of doc to "Hello"
set doc to document "Hello"
set text of doc to "ABC"
end tell
But I would suggest a text editor from this century. BBEdit is fantastic its technically older than TextEdit but its currently under development. It behaves as you would expect because it returns a ‘text document id #’ reference.
tell application "BBEdit"
activate
set doc to make new document with properties {name:"Info", text:""}
set name of doc to "Hello"
set text of doc to "ABC"--> "ABC"
end tell
@paulskinner
But I would suggest a text editor from this century. BBEdit is fantastic its technically older than TextEdit but its currently under development.
Yes I know it, I even bought the license 3 days ago and start digging (but vi shortcuts in fingers don’t help )
Anyway I started to check TextEdit as simple way to present output from script using monospaced fonts - the simple dialog displayed by AppleScript is bogus and it was first idea to have wider window with fixed fonts to presents partial results when script is running.
And of course good occasion to learn something new.
Anyway I started to check TextEdit as simple way to present output from script using monospaced fonts - the simple dialog displayed by AppleScript is bogus and it was first idea to have wider window with fixed fonts to presents partial results when script is running.
Is that so you can see what your script is doing in real time as it’s running?
If so that’s built in to that Script Debugger.
They’re no longer developing it, but the most recent version is now free (See link I posted above) and there’s plenty of support in the script debugger forums (which are run by the same guys that run macScripter (the developers of Script Debugger).
Check out this short video that show how to step through a script in Script Debugger line by line and see the result of each command in real time:
No, my script reads entries from spreadsheet (Numbers.app), draws one question, presents it on screen (with additional keywords, which should exist in answer for my convenience) and after response save partial assessment in column of spreadsheet. After 3 questions , calculate average and again presents on the screen.
The problem is with presenting question and keywords, because output in display dialog is ugly, without good formatting, so I thought about TextEdit
I think that I feel good with Script Debugger (it is one more after dbx, gdb, lldb), but co cepts in AppleScript sometimes amaze me.
BTW
I don’t see movie under link, only one image, and I don’t have accounts on Twitter and Facebook (only Mastodon) so couldn’t try to find it there also.
I’m not confident in my English, so I might not be fully understanding the original intent,
but I thought this might be helpful, so I’ll share it just in case.
This is a bit different from simply renaming an unsaved document,
but when I create a new document in TextEdit,
I make sure to specify the file path right away.
I don’t really know the reasoning behind it…
but somehow, things just seem to work more smoothly when I do that.
I hope this helps someone!
#!/usr/bin/env osascript
use AppleScript version "2.8"
use scripting additions
set aliasDesktopPath to (path to desktop from user domain) as alias
set strDesktopPath to (POSIX path of aliasDesktopPath) as text
#
set strFileName to ("Hello.html") as text
set strSaveFilePath to ("" & strDesktopPath & strFileName & "") as text
set aliasSaveFilePath to (POSIX file strSaveFilePath) as «class furl»
tell application "TextEdit"
activate
set refActivDoc to make new document with properties {name:strFileName, path:strSaveFilePath, text:""}
--> Hello.html
# save document strFileName in aliasSaveFilePath
end tell
set strChangeFileName to ("Blah.html") as text
set strSaveFilePath to ("" & strDesktopPath & strChangeFileName & "") as text
set aliasSaveFilePath to (POSIX file strSaveFilePath) as «class furl»
tell application "TextEdit"
tell refActivDoc
set name to strChangeFileName
end tell
set text of document strChangeFileName to "美しい日本語のHTML"
set text of document strChangeFileName to "Beautiful English HTML"
save document strChangeFileName in aliasSaveFilePath
close document strChangeFileName saving no
end tell
tell application "Finder"
open file aliasSaveFilePath
end tell
I just tried the following and it ran flawlessly.
I could see the name change (both with a delay and without it)
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "TextEdit"
activate
set doc to make new document with properties {name:"Info", text:""}
delay 5
set text of doc to "ABC"
set name of doc to "Hello"
end tell