Build own AppleScript numerical error handling - json

I'm wanting to build an app for validating projects using custom error numbers I've defined similar to:
try
## do something
on error number -2700
display dialog "Foobar"
end try
with the help of JSON Helper defining the list as:
tell application "JSON Helper"
set myJSON to make JSON from {-1232:"missing definition", -123231:"foo", -1232314:"bar" }
return myJSON
end tell
however I do not see a way to do this after referencing:
Error Numbers and Error Messages
Working with Errors
other then using a bloated conditional like:
try
open for access file "MyFolder:AddressData" with write permission
on error msg number n from f to t partial result p
if n = -49 then -- File already open error
display dialog "I'm sorry but the file is already open."
else
error msg number n from f to t partial result p
end if
end try
After researching I was unable to populate anything other than "What techniques work to handle errors in AppleScript so I can place a dialog?" so is there a way in AppleScript I can write error handling similar to Error Numbers and Error Messages documentation?

It's possible with a property list items.
This script put a record in a new property list item
Use the error number as string to get the value in the property list items
set myRecord to {|-1232|:"missing definition", |-123231|:"foo", |-1232314|:"bar", |-49|:"I'm sorry but the file is already open.", |-43|:"This file wasn’t found."}
tell application "System Events" to set myPlist to make new property list item with properties {kind:record, value:myRecord}
try
open for access file "MyFolder:AddressData" with write permission
on error number n
tell application "System Events" to set r to value of first property list item of myPlist whose its name is (n as text)
display alert r
end try
Question of JMichaelTX
Here's a script to save a property list items to a PLIST file (In the Preferences folder in this example).
set plistPath to (path to preferences folder as string) & "errorsMsgs.plist"
tell application "System Events"
set myPlist to make new property list item with properties {kind:record, value:myRecord}
make new property list file with properties {contents:myPlist, name:plistPath}
end tell

Related

Q: Writing CSV to variable location

Very new to AppleScript and have looked for solutions elsewhere unsuccessfully, apologies if I simply missed the answer elsewhere. Thanks for any help.
Background: I am using AppleScript to prompt the user for a directory, then create numerous folders in that directory with other prompts. As prompts are answered, I want to create a CSV that records these answers to prevent repeated answers and also allow the user to terminate the script and return later without losing items stored in lists. CSV seems the simplest way to do this.
Question- if I target the CSV to a static path I can successfully create the CSV and record to it. If I create a path from a prompt I receive the error: Finder got an error: Can’t make alias "Macintosh HD:Users:MyAccount:_Sample_Directory:" into type constant. number -1700 from alias "Macintosh HD:Users:MyAccount:_Sample_Directory:" to constant
What am I missing?
FYI- using this as basis for the WriteTo function.
Sample Script:
set MyList to {"A", "B", "C"}
tell application "Finder"
--This Works, but doesn't nest in the variable folder--
--set CSVFile to (path to desktop as text) & "List.csv"
--This is the goal:dynamic directory--
set ParentFolder to (choose folder with prompt "Select Folder")
set CSVFile to (path to ParentFolder as string & "List.csv") as POSIX file as alias
end tell
set TheResult to writeto(CSVFile, MyList, list, true)
on writeto(targetFile, theData, dataType, apendData)
try
set openFile to open for access file targetFile with write permission
if apendData is false then set eof of openFile to 0
write theData to openFile starting at eof as dataType
close access openFile
return true
on error
try
close access file targetFile
end try
return false
end try
end writeto
In your script, you do not need to tell application "Finder". So you can remove the tell / end tell.
The error in your script comes from line:
set CSVFile to (path to ParentFolder as string & "List.csv") as POSIX file as alias
Your sub-routine writeto accepts the targetfile to be in form of string like :
"disk:Users:me:folder:list.csv.
Therefore your first line could be reduced to :
set CSVFile to (ParentFolder as string) & "List.csv"
Yes, you can write your data in csv. But I am not sure it is the best choice. When you write the list {"A","B","C"} in your file, you are not writing text. So Applescript does a conversion from list to text. In this example the result is "listutxtAutxtButxtC". It depends what you want to do with that file, but to make it readable, it is better to write text in text file. You can do this by looping through all items of MyList to write each one as a text:
repeat with x in MyList
set TheResult to writeto(CSVFile, x, list, true)
end repeat
In above example, I still use datatype = list, instead of text. It does not matter because the subroutine does not use that parameter.I guess this is for future use.
Still it may not give you the result you're looking for. The text in file will be "ABC". To avoid this, I suggest you to add a return (or a tab) for each data you write in the file. Do to so, change the line in the writeto sub-routine :
write (theData & return) to openFile starting at eof as dataType
Then your cvs file will be
A
B
C
Which is easy to read.

SSIS - Error when passing object from one script task to another

I am passing object of a class from one script task to another. I have declared a package level variable by the name IPSService which is of type object.
The first script task contains the following piece of code
IPSService iPSService = new IPSService();
Dts.Variables["IPSService"].Value = iPSService;
I get an exception at the second line of code where the assignment happens.
The error message is as follows.
he element cannot be found in a collection. This error happens when you try to retrieve an element from a collection on a container during execution of the package and the element is not there.
Could somebody tell me what the issue is ?
You need to refer to the variable like this, notice the use of User:: in the variable name:
Dts.Variables["User::IPSService"].Value = iPSService;

ssis - capturing the bad rows

HI, Can you help me to figure this out? Is there a way to get the row in which error occured in ssis? I have this flat file with some 10k + records which is being read via a 'flatfilesource'.
Right now the error output defaults to error-column, error-code, and 'flatfilesourceerroroutputcolumn' - and i use a script-component to handle it. But none of these three inputs (to script component) are user-friendly enough. So i want to get an output like the first column-value(this is a unique identifier) of the row in which error occured. How can I add that?
While debugging this in SSIS, you can add a Data Viewer on the path to where your script handles error. This path has all the columns of the original row where your error is.
If you want to handle your SSIS errors and also do something else with it, you can direct the error output from your flat file source to a Multicast and then send one stream down to a file, a table, or something else ( a Recordset destination and a subsequent foreach loop on the object used to store the Recordset will let you do stuff on a row-by-row basis on the errored row(s)).

AppleScript syntax error

I trying to output a list of todo items from the Mac app Things using AppleScript.
But I get a syntax error:
Expected expression but found “to”.
Since Things uses the name "to dos" and AppleScript doesn't like this, since to is a reserved keyword. It works without a problem if the repeat code is directly inside the tell statement and not in the function handler.
Is there any way around this?
set output to ""
on getTodos(listName)
repeat with todo in to dos of list listName
set todoName to the name of todo
set output to output & todoName
end repeat
end getTodos
tell application "Things"
getTodos("Inbox")
getTodos("Today")
end tell
Is this even possible to do it like this?
And is there a better way to do this?
Sure, this is readily doable. The problem is that outside the tell application "Things" ... end tell block, AppleScript doesn't know what things would be special inside it, and doesn't even both to look. All you need to do, then, is move the tell block within on getTodos(listName) ... end getTodos:
on getTodos(listName)
tell application "Things"
repeat with todo in to dos of list "Inbox"
set todoName to the name of todo
set output to output & todoName
end repeat
end tell
end getTodos
You may also be able to replace tell with using terms from; I think that should work. Also, you never use listName—did you mean to replace "Inbox" with listName?
You should, however, be able to replace getTodos with the single line
on getTodos(listName)
tell application "Things" to get the name of the to dos of list listName
end getTodos
This sort of shortcut is one of the things AppleScript is good at. Also, note that this new version doesn't modify output, but just returns the list; I'd argue that's a better decision anyway, but you can always do set output to output & ....
I believe the issue is that AppleScript does not know you are trying to use Things-specific terminology in your handler. Try putting the repeat within a using terms from application Things block, like so:
on getTodos(listName)
using terms from application "Things"
repeat with todo in to dos of list "Inbox"
set todoName to the name of todo
set output to output & todoName
end repeat
end using terms from
end getTodos

Use a value from a combo/text box in a file path string

I have a form that I intend to allow users to browse powerpoint presentations through, because there are a lot of different briefs for metrics, which is what I use this database for. So, I already know how to set up everything, just got one little hang up.
If I place a combo box on the form to select from a list of different presentation, can I use that in the file path string (that I have to use to pull the ppt into theobject frame in access.
example:
"C:\Users\Justin\Desktop\" & cmbTitle & ".ppt"
I tried it and it gives me the error message variable not defined. I never defined a control before on in these things, would it be as a string?
I realize that the exact file path much match the entered value. Access 2000-2003/XP
Thanks as always guys!
You need to refer to the field as Me.cmbTitle. As it is written, it looks like you're calling the variable cmbTitle which doesn't exist.
Is the value of cmbTitle some ID/Integer field or is it the actual string value? You may want to use the immediate window to check this. Also, make sure the value of cmbTitle doesn't have any backslashes or spaces (That may require quotes?).
I'm somewhat confused as to what you're trying to do. I will write my answer assuming:
you have a form in an Access database.
on that form is combo box that lists the PowerPoint presentations your users are working with.
the bound column of the combo box lists the filename (without path) of each PPT file.
when the user selects a filename from the combo box, you want to display it in an unbound OLE object frame.
The code for that, assuming the list of PPT files is called cmbTitle, would be in the combo box's AfterUpdate event and would look like this:
Private Sub cmbTitle_AfterUpdate()
Dim strPresentation As String
If IsNull(Me!cmbTitle) Then Exit Sub
strPresentation = "C:\Users\Justin\Desktop\" & Me!cmbTitle & ".ppt"
Me!olePPT.SourceDoc = strPresentation
End Sub
Now, I can't get a test unbound OLE object frame to work with this, but it seems to me to be the right way to do it.
My suspicion is that you're either attempting to set the wrong property, or you've defined your OLE frame wrongly, but I can't offer any more advice on that without knowing more about what you're actually attempting to do, and exactly what line of code is causing the error.