Setting Property Value of Object in Applescript Objective-C - xcode

I am working in XCode (5) on OSX 10.9.5, creating an ApplescriptOjbC project.
I have created a new applescript in the project with a class (script) named "MSDropBox" and am using it to accept the drop of files. The files dropped have their file paths read correctly. I now just want to pass this array off to the Array Controller, which is used as the source for a table. I want the table to reflect the dragged files.
I have a method in the AppDelegate class that sets the value and I am calling it from the MSDropBox class. However, it is affecting the values in the table. I believe it is calling on the method of the class, but not object. How do I affect the object?
Below is the MSDropBox class:
script MSDropBox
property parent : class "NSBox"
property window : missing value
property thisPageList : missing value
on draggingEntered_(sender)
log "entered"
set pb to sender's draggingPasteboard()
set theOptions to {NSPasteboardURLReadingFileURLsOnlyKey:1}
return pb's canReadObjectForClasses_options_({current application's |NSURL|}, theOptions)
end draggingEntered_
on performDragOperation_(sender)
log "perform"
-- Get the file paths
set pb to sender's draggingPasteboard()
set theOptions to {NSPasteboardURLReadingFileURLsOnlyKey:1}
set theURLs to pb's readObjectsForClasses_options_({current application's |NSURL|}, theOptions)
log theURLs
repeat with thisURL in theURLs
set thisURLStr to (characters 1 thru -1 of ((thisURL's |path|()) as string) as string)
set thisPageList to thisPageList & thisURLStr as list
end repeat
return true
end performDragOperation_
on concludeDragOperation_(sender)
log "concludeDragOperation_"
tell class "AppDelegate" of current application
setPageList_(thisPageList)
end tell
end concludeDragOperation_
end script

In Objective-C it's
AppDelegate *appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
[appDelegate setPageList:thisPageList];
The AppleScriptObjC equivalent is
set appDelegate to current application's NSApplication's sharedApplication()'s delegate()
appDelegate's setPageList_(thisPageList)
I'm not sure whether AppleScriptObjC recognizes the application delegate method without type casting, maybe you have to add as AppDelegate

Related

MacOS: How to update an AppDelegate property via editing a table view cell?

In my macOS app I’m trying to update an AppDelegate property by editing a Table View Cell.
I created a sample project (Table View example project with array controller) using tips from the answer to this post.
I’m using macOS Big Sur (11.6.8) and Xcode 12.5.1. I'll be glad to supply a source archive download link or email it if requested. When I tried to add a link in this post it was rejected.
Here's my AppDelegate script:
script AppDelegate
property parent : class "NSObject"
property msg : ""
-- IBOutlets
property theWindow : missing value
property tableViewData : missing value
property arrayController : missing value
property showTableViewData : missing value
on `applicationWillFinishLaunching_(aNotification)`
appInit()
end applicationWillFinishLaunching_
on applicationShouldTerminate_(sender)
return current application's NSTerminateNow
end applicationShouldTerminate_
on appInit()
set my msg to (current date & return & return & "Initializing…") as string
delay .5
initTableView()
set my msg to (current date & return & return & "Ready.") as string
delay .5
end appInit
on initTableView()
set my tableViewData to {}
set my tableViewData to {{adName:("C21 ad" as string), pageNumber:("001" as string)}, {adName:("ERA ad" as string), pageNumber:("002" as string)}}
end initTableView
on `showTableViewData_(sender)`
set my msg to (current date & return & return & "showing tableViewData....") as string
delay .5
set displayText to ""
repeat with thisRecord in tableViewData
display alert "adName: " & (adName of thisRecord as string) & return & "pageNumber: " & (pageNumber of thisRecord as string)
end repeat
set my msg to (current date & return & return & "Ready.") as string
delay .5
end `showTableViewData_`
end script
How it works
As you can see I have hardcoded two records which I use to populate the tableViewData property on initialization which in turn is displayed in the table view. For some reason the adName data is not being displayed, but that bug is not what this post is about.
App initialized
Clicking the button reads the tableViewData property, iterates the records displaying them via an alert (so we can see it's contents).
Record display
Aside from the adName not displaying, so far so good.
Next, I edit the pageNumber table view cell for the first record (changing it from "001" to "777" and hitting return). When I click the button the first record is displayed which still shows a pageNumber value of "001" (instead of the "777" value I entered).
Record display after editing
Here are some shots of the table view cell attributes, connections, and bindings:
Attributes
Connections
Bindings
I tried selecting the bindings setting "Continuously Updates Value" but that doesn't seem to help; my tableViewData property is not updated with the newly entered data in the table view cell.
Most of the current developer docs use Obj-C or Swift instead of AppleScript and iOS or tvOS related. I'm using Xcode 12.5.1 because of problems binding UI elements to my code.
Thanks for taking the time to look this over.
The contents of tableViewData doesn't change but content of arrayController does. Try
repeat with thisRecord in content of arrayController
display alert "adName: " & (adName of thisRecord as string) & return & "pageNumber: " & (pageNumber of thisRecord as string)
end repeat
To make it work, use a Script Object instead of a record.
on makeAd(nameOfAd, pageNr)
script ad
property adName: nameOfAd
property pageNumber: pageNr
end script
return ad
end makeAd
on initTableView()
set my tableViewData to {makeAd("C21 ad", "001"), makeAd("ERA ad", "002")}
end initTableView

Mac OS X 10.6 Swift 4.2 Xcode 10.0: this class is not key value coding-compliant for the key

Situation: Mac OS X 10.6 Swift 4.2 Xcode 10.0 application. Data stored in Sqlite db which is then placed into an array for an array controller and tableview. The Object Controller for the ArrayController has the Mode set to Class and the Class Name is "Recording" (no quotes) with the following structure (in a swift file titled Recording):
#objc(Recording)
class Recording: NSObject {
#objc dynamic var user: UUID
#objc dynamic var recordingdate: Date
#objc dynamic var recordingtime: String
#objc dynamic var occurrence: String
#objc dynamic var reading: Int32
#objc dynamic var note: String
init(user: UUID, recordingdate: Date, recordingtime: String, occurrance: String, reading: Int32, note: String) {
self.user = user
self.recordingdate = recordingdate
self.recordingtime = recordingtime
self.occurrence = occurrance
self.reading = reading
self.note = note
}
}
The ArrayController is bound to the ViewController with the Model Key Path set to RecordingsArray which is an array of Recording objects.
The TableView Table Content is bound to the ArrayController with the Controller Key being set to arrangedObjects. There are 3 columns with each Table View Cell bound to their Table Cell View with the ModelKey Paths set to objectValue.aRecordingVar.
The issue is I keep getting the following error:
this class is not key value coding-compliant for the key RecordingsArray.
The Sqlite db has an entity called Recordings with the following attributes:
note: String
occurrence: String
recnum: Int32
recording: Int32
recordingdate: Date
recordingtime: String
user: UUID
I have cleaned and rebuilt the project with no affect and watched numerous youtube videos and google searches about this subject with no deviation from the process. Does anyone have any ideas on what's wrong?
Larme, thanks for the replies, it made me take a closer look at my assignments and I found that I had the wrong ViewController swift file assinged the nib file in the Storyboard. Everything works now.

Binding and NSPopUpButton: changing the value of the current key doesn't change the selection

I've a really simple UI with a single NSPopUpButton. Its selectedIndex is bound to an int value ViewController.self.my_selection. As soon as I change the selection from the UI (i.e. a select the third item of the NSPopUpButton) I see that my_selection value changes. So far so good, what I'm trying to obtain is the opposed direction though. I want to change the my_selection value programmatically and see the NSPopUpButton selecting the item a the index that I've defined in my_selection. I erroneously supposed that behaviour was the default behaviour for bindings...
This is what I'm obtaining now:
NSPoPUpButton ---> select item at index 2 ----> my_selection becomes equal to 2
This is what I want to achieve (keeping also the previous behaviour)
my_selection ---> set value to 3----> NSPoPUpButton selected index = 3
Without a bit more info (see my comment) it's hard to see exactly what you're doing wrong. Here's how I got it working: First, create a simple class...
// Create a simple class
class Beatle: NSObject {
convenience init(name: String) {
self.init()
self.name = name
}
dynamic var name: String?
}
Then, in the AppDelegate I created a four-item array called beatles:
dynamic var beatles: [Beatle]?
func applicationDidFinishLaunching(aNotification: NSNotification) {
beatles = [Beatle(name: "John"),
Beatle(name: "Paul"),
Beatle(name: "Ringo"),
Beatle(name: "George")]
}
In Interface Builder I set things up so that this array provides the pop-up with its content:
This class also has a selectedIndex property that is bound to the pop-up button's selectedIndex binding - this property provides read-write access to the pop-up button's selection:
// Set the pop-up selection by changing the value of this variable.
// (Make sure you mark it as dynamic.)
dynamic var selectedIndex: Int = 0

How can I bind the text in a NSTextField to the value of a property on a controller using a storyboard?

I have a custom view with two text subviews, arranged, not that it matters, as per this amazing ASCII art:
/--------\
| lblOne |
| lblTwo |
\--------/
On my controller, I have a property of type Thingy:
class AwesomeController: NSViewController {
var thingy: Thingy! = nil
}
A Thingy has two properties of interest:
class Thingy: NSObject {
var one: String
var two: String
}
I would like to set up a binding between lblOne's string value and thingy.one, and lblTwo's string value and thingy.two, going through a custom view class if necessary.
When thingy is changed, obviously the two text fields should also change. (In other words, it should behave normally for a cocoa binding.)
I think it's probably a combination of learning Swift and my unfamiliarity with storyboards on OS X (last time I did cocoa development, it was still xibs), but I can't work out how to link the damn thing up.
Getting bindings to work in Swift requires two additional steps:
All the vars must be marked with dynamic, eg:
dynamic var one: String
You have to recompile the project (with cmd+B, not just in the background) in order for the var to appear as an option in IB

UITextInputDelegate methods not functioning correctly

I'm working on iOS 8 custom keyboard extension right now, and there are some issues in using UITextInputDelegate Methods.
Does this right: selectionWillChange: and selectionDidChange: methods should be called when user long-presses typing area? And textWillChange: and textDidChange: methods should be called whenever the text is literally changing?
Actually, what I observed is that, when I changed selection in text input area, textWillChange: and textDidChange: are called, and I cannot get a clue that the other two methods are called in what condition. If anyone knows about the usage of these delegate methods, please let me know.
It's not working for me either... what I am currently doing is just using textWillChange and textDidChange which does get called, as you mentioned, when you change your selection... (they get called BEFORE and AFTER) And then comparing the: self.textDocumentProxy.documentContextBeforeInputself.textDocumentProxy.documentContextAfterInput From BEFORE (textWillChange) to the AFTER (textDidChange) to see if selection range or length changed at all.
Something like this (set the 4 NSStrings below in your .h file of course... haven't tested this exact snippet because I wrote it from scratch just now on SO.com but I'm sure the principle works if I made any errors)
- (void)textWillChange:(id<UITextInput>)textInput {
beforeStringOfTextBehindCursor = self.textDocumentProxy.documentContextBeforeInput;
beforeStringOfTextAfterCursor = self.textDocumentProxy.documentContextAfterInput;
}
- (void)textDidChange:(id<UITextInput>)textInput {
afterStringOfTextBehindCursor = self.textDocumentProxy.documentContextBeforeInput;
afterStringOfTextAfterCursor = self.textDocumentProxy.documentContextAfterInput;
BOOL didSelectionChange = NO;
if (![beforeStringOfTextBehindCursor isEqualToString:afterStringOfTextBehindCursor]) {
didSelectionChange = YES;
}
if (![beforeStringOfTextAfterCursor isEqualToString:afterStringOfTextAfterCursor]) {
didSelectionChange = YES;
}
if (didSelectionChange) {
NSLog(#"Selection Changed!");
}
}
I had the same problem with the functions specified in the UITextInput protocol not being called. The reason as far as I can discern is due to the fact that the inputDelegate is set at runtime. According to the ios docs:
The UIKit provides a private text input delegate, which it assigns at runtime to the inputDelegate property of the object whose class adopts the UITextInput protocol. link
The fix which works in my case is to reset the inputDelegate in the function:
textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string
by the following line:
[myUITextField setInputDelegate:self];
where self implements the UITextInputDelegate protocol.

Resources