‘use scripting additions’ breaks text item delimiters

I must use two versions of an app to accomplish what I intend with each version having a different set of AppleScript commands. The app is Pages. The obstacle is that since my script references Standard Additions’ items, it won’t compile unless I add the clause use scripting additions.

However, they pulled their tail out of the mud to have their head stuck in the mud. Adding the clause breaks AppleScript’s text item delimiters. The compiler marks the term as one of the Scripting Additions classes instead of registering it as AppleScript’s property.

The outline of my code is as follows:

use application "Pages" version "5.2.2"
use scripting additions

property A: some_value

choose file name other_parameters

set AppleScript's text item delimiters

_code

tell application "Pages"

_the_rest_of_the_code.

Is there a solution?

The issue is that Pages has a text item class. The simplest solution is to delete both of the use statements, but the following also appears to fix the issue:

use application "Pages" version "5.2.2" -- causes error
--use application "Pages" version "5.2.2" without importing -- doesn't cause error
use scripting additions

display dialog "Hello" -- requires acripting additions statement
set theString to "This is a test"
set AppleScript's text item delimiters to {" "}
set theTextItems to text items of theString

That’s where the ambiguity lies. I came across without importing but felt dumbfounded reading the relevant passage in the official AppleScript documentation written in obtuse shaman talk.

Importing happens by default, but can be suppressed using the without importing parameter, if applicable. You can use this to add requirements to existing scripts without changing anything else about the script

Why would we use it all if it doesn’t import app’s terminology? If the terminology isn’t imported, which I interpret as the dictionary not loaded, how do I plug in the app’s commands? How do I know if the script uses the version I want?

OK, so a new problem has arisen on the shoulders of the old one. When the script compiles for one app, the other one gets locked in the terminology of the former, and vice versa. The circle closed.

Any ideas?

Update. The format of script document 1 is text, and script document 2 is a compiled script. I relaunched Script Editor making the script document 1 open to an uncompiled state and now I’m getting the compile time error

Syntax Error
Can’t get application “Pages” whose version ≥ “5.2.2”.

Bummer!

scrutinizer82. I agree that the AppleScript Language Guide is not clear as to what the without importing parameter does. However, it clearly fixes the issue you originally asked about, which arises from a terminology conflict.

You cannot specify a particular version of Pages, but you can specify a minimum version. Given that Pages 5.2.2 was released 10 years ago, requiring a minimum version would not seem particularly useful.

Commands pertinent to the Pages app are normally executed within a tell statement, and there’s typically no need for the use application “Pages” statement. Unless you have some other reason for doing so, I would omit that statement.

It would be helpful if you posted the script, or portion of the script, that is causing the issue. Otherwise it’s difficult to be of help.

I think I got to the bottom. During the hours that have passed since my last post, I went through various vicissitudes of gaining and losing insights: I rarely included the use declarations, so I came unprepared. However that may be, the mechanism seems to be underdeveloped, lacking in flexibility and effectiveness.

One attempt of mine was to re-form the application “Pages” specifier to assign the POSIX path as its name, while leaving use…without importing and use scripting additions alone. That led to the formatting of all the commands, except text item delimiters, as those of scripting additions, which crippled my code.

The ultimate solution was to remove setting the default application to open the newly saved document via System Events.

I use both very old and relatively new software, straddling the Pages series from Pages 4.2 to Pages 10.1. One of my computers has Pages 4 and Pages 5 alongside one another. Pages 4 outclasses Pages 5 in its offer of ways to manipulate a layout, so I keep using it for more elaborate tasks.

Removing the aforementioned System Events command renders both use statements unnecessary.

What’s the practicality of importing terminology if it’s hit and miss?

1 Like

scrutinizer82. I’m glad you found a solution. Just as a minor point of information, it’s getting text items, not setting text item delimiters, that causes the issue. The following script–which sets text item delimiters and works as expected–demonstrates this:

use application "Pages" version "5.2.2"
use scripting additions

display dialog "Hello" -- requires acripting additions statement
set theList to {"one", "two", "three"}
set AppleScript's text item delimiters to {" "}
set theText to theList as text --> "one two three"
set AppleScript's text item delimiters to {""}
set theText to theList as text --> "onetwothree"

It seems that Pages has an issue with its bundle metadata and extension. Unlike QuickTime 10 and QuickTime 7, you can’t refer to different versions of Pages by the bundle ID. When you set the file associations for a Pages document created in one version, it automatically applies the same association to Pages documents created with other versions. This causes problems with scripting, as setting the default application to an older version of Pages via Get Info in Finder doesn’t always work.

Even after repeated attempts, the document may still open in the latest version of Pages.

The User Interaction Suite commands with the use clauses present aren’t the biggest issue, but the compiler converts document formatting and layout manipulation properties of Pages 4 to the corresponding pseudo properties of Scripting Additions. It’s unclear which one of these on my system (if any) has these properties, commands, and classes. As a consequence, the script fails to actuate the statements.

Text2Table in Pages 5.applescript (9.6 KB)

Text2Table in Pages 4.applescript (8.5 KB)

Hi.

You can use the path to the relevant version as the target of the ‘tell’ statements:

property default_app_path : POSIX path of (path to applications folder) & "iWork '09/Pages '09.app"
-- Or:
-- property default_app_path : (path to applications folder as text) & "iWork '09:Pages '09.app"

tell application default_app_path
	
	-- Relevant Pages code here.
	
end tell

But it’s a good idea to make sure the “other” version is closed before you compile or run the script. If I remember correctly, if an older version of an app is open and the latest one isn’t, this overrides AppleScript’s preference for the latest version, which may also help.