OK, so I have created the skeliton of my applescript objective c app. I have my properties declared
and I have a button in my .xib
on CreateXML_(sender)
end CreateXML_
I want this button to gather all of the text of the text fields that are my properties. Im not sure how to do this being that I can refer to these text fields via applescript name anymore.
Can somebody help me out?
Are you saying that these properties are all IBOutlets for your text fields? And, what do you mean by “gather all the text” – what form do you want this data to be in when you get it?
You can refer to these text fields by name if the properties you listed are the IBOutlets for them.
Ric
Yes they are outlets. Im looking to generate a text file with the values eventually. Right now I am having trouble getting the values of the text fields.
I tried
Set theJobName to contents of jobName
display dialog jobname
when I do this, I get no errors and no display dialog message.
Can you help?
set theJobName to jobName’s stringValue()
Most of the methods for setting and getting values from NSTextField are listed in the NSControl class reference, which NSTextField inherits from.
Ric
thanks ric, one other question.
After i gather all of the text field values Im trying to create a new file and write the data to that file. its not working.
This is the subroutine I always used in applescript. Do I need to use a different routine?
on Create_File(theData)
set targetFile to (path to desktop) & "test.txt" as string
set the openfile to open for access file targetFile with write permission
display dialog "here"
write theData to openfile starting at eof
close access the openfile
display dialog "done" giving up after 1
end Create_File
I don’t have much experience with regular applescript, so I can’t say if there is anything wrong with what you posted. There are several different ways to save data in ASOC and which one to use depends on what kind of data you are saving. So, what is theData? Is it text? Is it an array? Is it an NSData object?
Ric
It will be text. What I would usually do is gather my text fields into variables. Format it as xml or JDF and then pass that formatted variable to my sub routine as (thedata) which creates a file, populates it with the final formated text and saves the file.
Thanks for the help ric
You’re hitting one of the quirks if ASObjC, where it doesn’t like terms like file and alias used as specifiers. The solution in this case is to address the application:
Tell current application to set the openfile to open for access file targetFile with write permission
Thanks Shane, works like a charm
Spoke to early, It works fine when using path to desktop. But I am actually trying to write the file to a folder named “test” on a mounted volume called PackContainer
This is what I am trying
on Create_XML(theData)
set theFolder to "/Volumes/PackContainer/test"
set theFolder to POSIX file theFolder
set targetFile to theFolder & ":test.xml" as string
tell current application to set the openfile to open for access file targetFile with write permission
write theData to openfile starting at eof
close access the openfile
end Create_XML
Any ideas?
How about:
set theFolder to "Volumes:PackContainer:test:"
set targetFile to POSIX path of (theFolder & "test.xml" as string)
If you log targetFile you get: /Volumes/PackContainer/test/test.xml
Ric
that fine, but Im still having a problem with the next line. Not sure why. don’t get a error that I know of and it does not make a output xml file.
tell current application to set the openfile to open for access file targetFile with write permission
write theData to openfile starting at eof
I think it should be:
tell current application to set the openfile to open for access targetFile with write permission
You have an extra word “file” in there.
Ric
If you use “POSIX path”, you don’t need the word “file” – or the tell current application. You need them both if you use an HFS path.
Thanks Guys, that part is working now.
Shane, I downloaded and used your AppleScriptObjC_Explorer. Very nice and thank you. Great tool.
I do have one other question. My app is designed to gather information from fields in the .xib and write those to a xml file naming it the “jobname.xml” that was defined in one of the text fields. This is working great. The problem i have is that it will not make a second new xml file when I change the name and hit the submit button again. Its like the script needs to be reset or something?
Any Idea’s?
Here is my code, sorry for the mess
--
property NSImage : class "NSImage" of current application
script CMSA_XMLAppDelegate
property parent : class "NSObject"
property orderID : missing value
property subOrderID : missing value
property JobName : missing value
property JobDescription : missing value
property dueDate : missing value
property csrName : missing value
property csrEmail : missing value
property customerID : missing value
property customerName : missing value
property customerContact : missing value
property customerEmail : missing value
property proof : missing value
property proofType : missing value
property press : missing value
property substrate : missing value
property trapping : missing value
property trapDistance : missing value
property workflow : missing value
property imageView : missing value
property imagePath : missing value
property xmlhotfolder : missing value
property prefWindow : missing value
property savePref : missing value
property closePref : missing value
property openPrefwindow : missing value
property fileName : missing value
-- Choose the file to include and generate preview for NSImageWell
on insertImage_(sender)
set ImageFile to choose file with prompt "Select Production File"
tell application "Finder" to set imageName to name of ImageFile
set ImageFile to POSIX path of (ImageFile as string)
imagePath's setStringValue_(imageName)
set newImage to NSImage's alloc()'s initWithContentsOfFile_(ImageFile)
imageView's setImage_(newImage)
end insertImage_
-- Button was clicked to gather field info, populate xml string and create a xml file
on CreateXML_(sender)
set orderID to orderID's stringValue() as string
set subOrderID to subOrderID's stringValue() as string
set JobName to JobName's stringValue() as string
set JobDescription to JobDescription's stringValue() as string
set dueDate to dueDate's stringValue() as string
set csrName to csrName's stringValue() as string
set csrEmail to csrEmail's stringValue() as string
set customerID to customerID's stringValue() as string
set customerName to customerName's stringValue() as string
set customerContact to customerContact's stringValue() as string
set customerEmail to customerEmail's stringValue() as string
set proof to proof's titleOfSelectedItem() as string
set proofType to proofType's titleOfSelectedItem() as string
set press to press's stringValue() as string
set substrate to substrate's titleOfSelectedItem() as string
set trapping to trapping's titleOfSelectedItem() as string
set trapDistance to trapDistance's titleOfSelectedItem() as string
set workflow to workflow's titleOfSelectedItem() as string
set fileName to imagePath's stringValue() as string
set theXML to "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><CMSARESULT><ERRORCODE>0</ERRORCODE><DATABASE>Captain Morgans Database.CMSADB</DATABASE><LAYOUT></LAYOUT><ROW MODID=\"1\" RECORDID=\"6\"><OrderID>" & orderID & "</OrderID><SubOrderID>" & subOrderID & "</SubOrderID><JobName>" & JobName & "</JobName><JobDescription>" & JobDescription & "</JobDescription><DueDate>" & dueDate & "</DueDate><CSRName>" & csrName & "</CSRName><CSREmail>" & csrEmail & "</CSREmail><CustomerID>" & customerID & "</CustomerID><CustomerName>" & customerName & "</CustomerName><CustomerContact>" & customerContact & "</CustomerContact><CustomerEmail>" & customerEmail & "</CustomerEmail><Proof>" & proof & "</Proof><ProofType>" & proofType & "</ProofType><Press>" & press & "</Press><Substrate>" & substrate & "</Substrate><Trapping>" & trapping & "</Trapping><Trap_Distance>" & trapDistance & "</Trap_Distance><Workflow>" & workflow & "</Workflow><FileName>" & fileName & "</FileName></ROW></CMSARESULT>"
set theFolder to "Volumes:PackContainer:XML_In:"
set targetFile to POSIX path of (theFolder & JobName & ".xml" as string)
tell current application to set the openfile to open for access targetFile with write permission
write theXML to openfile starting at eof
close access the openfile
end CreateXML_
on applicationWillFinishLaunching_(aNotification)
-- Insert code here to initialize your application before any files are opened
-- Preset the due date field to be 1 month from current date
set AppleScript's text item delimiters to "/"
set dateItems to text items of (short date string of (current date))
set AppleScript's text item delimiters to ""
set theDate to ((item 1 of dateItems) + 1) & "/" & item 2 of dateItems & "/20" & item 3 of dateItems as string
dueDate's setStringValue_(theDate)
end applicationWillFinishLaunching_
on applicationShouldTerminate_(sender)
-- Insert code here to do any housekeeping before your application quits
return current application's NSTerminateNow
end applicationShouldTerminate_
end script
It doesn’t work because you’ve redefined jobName from the outlet for a textfield to the value for that text field with the statement: set JobName to JobName’s stringValue() as string
So, the second time you push the button you can’t get the stringValue of jobName, because it’s applescript text now, not a text field outlet. You need to give that variable a different name like jobNameValue or something.
You’ll have that same problem with all the outlet properties, so you need to change them all.
Ric
I recommend to use HFS path anyway and add some error handling in case the file couldn’t be opened.
As the XML file is indicated to be UTF8 encoded it should be written out with this encoding.
I hope the the «class .» construct works in an ASOC environment.
set theFolder to "PackContainer:XML_In:"
set targetFile to (theFolder & JobName & ".xml") as string
try
tell current application to set openfile to open for access file targetFile with write permission
write theXML to openfile as «class utf8» starting at eof
close access openfile
on error
try
close file targetFile
end try
end try
Note: the code appends the text to an existing file if it will be reused