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

Warhaven

macrumors newbie
Original poster
Mar 3, 2010
19
1
So I have something very simple (using objc-appscript framework):

Code:
if ([pagesApp isRunning]) {
	PGReference *realRef = [pagesApp documents];

	if( [realRef modified] ) {
		NSLog(@"Doc modified... saving.");
		[[realRef save] send];
	}
}

And even when the documents were recently saved and nothing's been done to them, [realRef modified] is still returning YES...

Any thoughts? Not quite sure what I'm doing wrong here...
 
So I have something very simple (using objc-appscript framework):

Code:
if ([pagesApp isRunning]) {
	PGReference *realRef = [pagesApp documents];

	if( [realRef modified] ) {
		NSLog(@"Doc modified... saving.");
		[[realRef save] send];
	}
}

And even when the documents were recently saved and nothing's been done to them, [realRef modified] is still returning YES...

Any thoughts? Not quite sure what I'm doing wrong here...

1. Appscript doesn't do 'implicit gets' as AppleScript does. You need to send a 'get' event to Pages to get the value of the 'modified' property. Longhand would be:

Code:
[[[realRef modified] get] send]

but this can be shortened to:

Code:
[[realRef modified] getItem]

2. Appscript uses its own ASBoolean class to represent Boolean values, so remember to call -boolValue on an ASBoolean instance to get a BOOL.

As your code is asking for the modified values of all documents, you're going to get an NSArray of ASBoolean instances, so you'll have to go through and check each one in turn.

Also remember that ObjC exceptions are reserved for serious (i.e. programmer) errors only. Normal application errors are indicated by nil results and/or NSError** args, so check those as needed.


BTW, you might run into problems with your 'save' command if any of the documents are newly created and haven't previously been saved to disk. (I think Cocoa apps tend to throw up Save dialogs in this situation, which isn't very clever.) But that's a general application scripting issue, not an appscript-specific one.
 
Thank you for the reply. I've been working on it some more, and I've solved both problems. I don't really need to check if the document has been modified or not in order to the send the save command, so I've simply done away with that.

The saving issue I resolved by doing the following:

Code:
PGReference *realRef = [pagesApp documents];
NSArray *pathArray = [NSArray arrayWithArray: [[realRef path] getItem]];
		
for (int i = 0; i < [pathArray count]; i++) {
	// [PGConstant missingValue] == New Document
	if([[pathArray objectAtIndex:i] isNotEqualTo:[PGConstant missingValue]]) {
		// Do something
	}
}

I just grab all the paths to the open documents. If it returns a missing value, then I know it's a fresh document.

[edit]

Oh, i see what you mean by the save dialog causing a problem. It stalls the thread until the user has finished dealing with it. I think what I'll do is sort the array and save everything that has a path right away, and then deal with the items that don't have one last or start a thread pool and assign a thread to each document attempting to save so as not to hold up everything.

Now I just need to learn how to do thread pools. :) Unless you have a better suggestion for handling it...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.