Manipulating Excel from a sandboxed OSX app

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Manipulating Excel from a sandboxed OSX app

Maarten Mostert-2
According to the Entitlement rules for Sandboxed OS X application it is possible to define scripting targets for your application.


The thing is that you cannot run an applescript with the osascript command as osascript is not entitled to run whatever script you want.
A solution (I thought) here is to compile your applescript into a standalone application and to sign it with the appropriate entitlements which appeared to work well for copying a cairo png image into the clipboard of Preview.

Obviously doing the same with Excel is a bit more complicated as you need to feed the compiled script with the data you want to put into Excel which is more work (see the script in the end).

Having done al that it still appears not to work, I get Sandbox collisions in the console and apple dev forums basically indicate me that this is allowed in only some rare circonstances like with mail and preview.

They indicate that the « official » way to use applescript from your application is to call it with NSAppleScript 

A full working code example stated to be able to run Excel from your app is given here:

- (NSString *) getStringFromAppleScript:(NSString *)theAppleScriptText {
     NSDictionary *errorDict= nil;
  NSAppleScript *appleScriptObject = [[NSAppleScript alloc] initWithSource:theAppleScriptText];
  NSAppleEventDescriptor *eventDescriptor = [appleScriptObject executeAndReturnError: &errorDict];
     if (([eventDescriptor descriptorType]) && (errorDict==nil)) {
     return [eventDescriptor stringValue];
  } else {
          return nil;
  }   

}

If there is anyone who knows how to implement the above snipet, please don’t hesitate to contact me.

Regards,

@+Maarten


For your information the applescript I used to read the csv tables for excel.

set aHome to "/Users"
set aUser to system attribute "USER"

set aFileName to "Library/Containers/com.stakepoint.portfolio/Data/Library/Application Support/StakePoint/tabledata.csv"
set totalName to aHome & "/" & aUser & "/" & aFileName
--display dialog totalName
--set theFileContents to (read POSIX file totalName)

(* Assumes that the CSV text adheres to the convention:
   Records are delimited by LFs or CRLFs (but CRs are also allowed here).
   The last record in the text may or may not be followed by an LF or CRLF (or CR).
   Fields in the same record are separated by commas (unless specified differently by parameter).
   The last field in a record must not be followed by a comma.
   Trailing or leading spaces in unquoted fields are not ignored (unless so specified by parameter).
   Fields containing quoted text are quoted in their entirety, any space outside them being ignored.
   Fields enclosed in double-quotes are to be taken verbatim, except for any included double-quote pairs, which are to be translated as double-quote characters.

       

   No other variations are currently supported. *)

on csvToList(csvText, implementation)
-- The 'implementation' parameter must be a record. Leave it empty ({}) for the default assumptions: ie. comma separator, leading and trailing spaces in unquoted fields not to be trimmed. Otherwise it can have a 'separator' property with a text value (eg. {separator:tab}) and/or a 'trimming' property with a boolean value ({trimming:true}).
set {separator:separator, trimming:trimming} to (implementation & {separator:",", trimming:false})


script o -- Lists for fast access.
property qdti : getTextItems(csvText, "\"")
property currentRecord : {}
property possibleFields : missing value
property recordList : {}
end script


-- o's qdti is a list of the CSV's text items, as delimited by double-quotes.
-- Assuming the convention mentioned above, the number of items is always odd.
-- Even-numbered items (if any) are quoted field values and don't need parsing.
-- Odd-numbered items are everything else. Empty strings in odd-numbered slots
-- (except at the beginning and end) indicate escaped quotes in quoted fields.


set astid to AppleScript's text item delimiters
set qdtiCount to (count o's qdti)
set quoteInProgress to false
considering case
repeat with i from 1 to qdtiCount by 2 -- Parse odd-numbered items only.
set thisBit to item i of o's qdti
if ((count thisBit) > 0) or (i is qdtiCount) then
-- This is either a non-empty string or the last item in the list, so it doesn't
-- represent a quoted quote. Check if we've just been dealing with any.
if (quoteInProgress) then
-- All the parts of a quoted field containing quoted quotes have now been
-- passed over. Coerce them together using a quote delimiter.
set AppleScript's text item delimiters to "\""
set thisField to (items a thru (i - 1) of o's qdti) as string
-- Replace the reconstituted quoted quotes with literal quotes.
set AppleScript's text item delimiters to "\"\""
set thisField to thisField's text items
set AppleScript's text item delimiters to "\""
-- Store the field in the "current record" list and cancel the "quote in progress" flag.
set end of o's currentRecord to thisField as string
set quoteInProgress to false
else if (i > 1) then
-- The preceding, even-numbered item is a complete quoted field. Store it.
set end of o's currentRecord to item (i - 1) of o's qdti
end if


-- Now parse this item's field-separator-delimited text items, which are either non-quoted fields or stumps from the removal of quoted fields. Any that contain line breaks must be further split to end one record and start another. These could include multiple single-field records without field separators.
set o's possibleFields to getTextItems(thisBit, separator)
set possibleFieldCount to (count o's possibleFields)
repeat with j from 1 to possibleFieldCount
set thisField to item j of o's possibleFields
if ((count thisField each paragraph) > 1) then
-- This "field" contains one or more line endings. Split it at those points.
set theseFields to thisField's paragraphs
-- With each of these end-of-record fields except the last, complete the field list for the current record and initialise another. Omit the first "field" if it's just the stub from a preceding quoted field.
repeat with k from 1 to (count theseFields) - 1
set thisField to item k of theseFields
if ((k > 1) or (j > 1) or (i is 1) or ((count trim(thisField, true)) > 0)) then set end of o's currentRecord to trim(thisField, trimming)
set end of o's recordList to o's currentRecord
set o's currentRecord to {}
end repeat
-- With the last end-of-record "field", just complete the current field list if the field's not the stub from a following quoted field.
set thisField to end of theseFields
if ((j < possibleFieldCount) or ((count thisField) > 0)) then set end of o's currentRecord to trim(thisField, trimming)
else
-- This is a "field" not containing a line break. Insert it into the current field list if it's not just a stub from a preceding or following quoted field.
if (((j > 1) and ((j < possibleFieldCount) or (i is qdtiCount))) or ((j is 1) and (i is 1)) or ((count trim(thisField, true)) > 0)) then set end of o's currentRecord to trim(thisField, trimming)
end if
end repeat


-- Otherwise, this item IS an empty text representing a quoted quote.
else if (quoteInProgress) then
-- It's another quote in a field already identified as having one. Do nothing for now.
else if (i > 1) then
-- It's the first quoted quote in a quoted field. Note the index of the
-- preceding even-numbered item (the first part of the field) and flag "quote in
-- progress" so that the repeat idles past the remaining part(s) of the field.
set a to i - 1
set quoteInProgress to true
end if
end repeat
end considering


-- At the end of the repeat, store any remaining "current record".
if (o's currentRecord is not {}) then set end of o's recordList to o's currentRecord
set AppleScript's text item delimiters to astid


return o's recordList
end csvToList

-- Get the possibly more than 4000 text items from a text.
on getTextItems(txt, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set tiCount to (count txt's text items)
set textItems to {}
repeat with i from 1 to tiCount by 4000
set j to i + 3999
if (j > tiCount) then set j to tiCount
set textItems to textItems & text items i thru j of txt
end repeat
set AppleScript's text item delimiters to astid


return textItems
end getTextItems

-- Trim any leading or trailing spaces from a string.
on trim(txt, trimming)
if (trimming) then
repeat with i from 1 to (count txt) - 1
if (txt begins with space) then
set txt to text 2 thru -1 of txt
else
exit repeat
end if
end repeat
repeat with i from 1 to (count txt) - 1
if (txt ends with space) then
set txt to text 1 thru -2 of txt
else
exit repeat
end if
end repeat
if (txt is space) then set txt to ""
end if


return txt
end trim

set csvText to (read POSIX file totalName)
set myrecords to csvToList(csvText, {})
set mymax to count myrecords


tell application "Microsoft Excel"
activate
make new workbook
set newBook to make new workbook
set update remote references of newBook to true
activate object worksheet 1
tell worksheet 1 of active workbook
set font size of font object of row 1 to 14
set value of cell "A1" to ""


repeat with n from 1 to mymax


set cmycell to item n of myrecords
set aaa to item 1 of cmycell
set bbb to item 2 of cmycell
set value of cell aaa to bbb


end repeat


set weight of (get border of range "$A$1:$Q$1" which border edge bottom) to border weight medium


set color of interior object of range "$A$1:$Q$1" to {235, 249, 255}
end tell
end tell



_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: Manipulating Excel from a sandboxed OSX app

Karsten Kusche
Hi Maarten,

if i remember correctly, this may contain some answers to your questions: http://www.objc.io/issue-14/sandbox-scripting.html

Kind Regards
Karsten



-- 
Karsten Kusche - Dipl. Inf. (FH) - [hidden email]
Georg Heeg eK - Köthen
Handelsregister: Amtsgericht Dortmund A 12812 

Am 11. Mai 2015 bei 13:16:56, Maarten Mostert ([hidden email]) schrieb:

According to the Entitlement rules for Sandboxed OS X application it is possible to define scripting targets for your application.


The thing is that you cannot run an applescript with the osascript command as osascript is not entitled to run whatever script you want.
A solution (I thought) here is to compile your applescript into a standalone application and to sign it with the appropriate entitlements which appeared to work well for copying a cairo png image into the clipboard of Preview.

Obviously doing the same with Excel is a bit more complicated as you need to feed the compiled script with the data you want to put into Excel which is more work (see the script in the end).

Having done al that it still appears not to work, I get Sandbox collisions in the console and apple dev forums basically indicate me that this is allowed in only some rare circonstances like with mail and preview.

They indicate that the « official » way to use applescript from your application is to call it with NSAppleScript 

A full working code example stated to be able to run Excel from your app is given here:

- (NSString *) getStringFromAppleScript:(NSString *)theAppleScriptText {
     NSDictionary *errorDict= nil;
  NSAppleScript *appleScriptObject = [[NSAppleScript alloc] initWithSource:theAppleScriptText];
  NSAppleEventDescriptor *eventDescriptor = [appleScriptObject executeAndReturnError: &errorDict];
     if (([eventDescriptor descriptorType]) && (errorDict==nil)) {
     return [eventDescriptor stringValue];
  } else {
          return nil;
  }    

}

If there is anyone who knows how to implement the above snipet, please don’t hesitate to contact me.

Regards,

@+Maarten


For your information the applescript I used to read the csv tables for excel.

set aHome to "/Users"
set aUser to system attribute "USER"

set aFileName to "Library/Containers/com.stakepoint.portfolio/Data/Library/Application Support/StakePoint/tabledata.csv"
set totalName to aHome & "/" & aUser & "/" & aFileName
--display dialog totalName
--set theFileContents to (read POSIX file totalName)

(* Assumes that the CSV text adheres to the convention:
   Records are delimited by LFs or CRLFs (but CRs are also allowed here).
   The last record in the text may or may not be followed by an LF or CRLF (or CR).
   Fields in the same record are separated by commas (unless specified differently by parameter).
   The last field in a record must not be followed by a comma.
   Trailing or leading spaces in unquoted fields are not ignored (unless so specified by parameter).
   Fields containing quoted text are quoted in their entirety, any space outside them being ignored.
   Fields enclosed in double-quotes are to be taken verbatim, except for any included double-quote pairs, which are to be translated as double-quote characters.

       

   No other variations are currently supported. *)

on csvToList(csvText, implementation)
-- The 'implementation' parameter must be a record. Leave it empty ({}) for the default assumptions: ie. comma separator, leading and trailing spaces in unquoted fields not to be trimmed. Otherwise it can have a 'separator' property with a text value (eg. {separator:tab}) and/or a 'trimming' property with a boolean value ({trimming:true}).
set {separator:separator, trimming:trimming} to (implementation & {separator:",", trimming:false})


script o -- Lists for fast access.
property qdti : getTextItems(csvText, "\"")
property currentRecord : {}
property possibleFields : missing value
property recordList : {}
end script


-- o's qdti is a list of the CSV's text items, as delimited by double-quotes.
-- Assuming the convention mentioned above, the number of items is always odd.
-- Even-numbered items (if any) are quoted field values and don't need parsing.
-- Odd-numbered items are everything else. Empty strings in odd-numbered slots
-- (except at the beginning and end) indicate escaped quotes in quoted fields.


set astid to AppleScript's text item delimiters
set qdtiCount to (count o's qdti)
set quoteInProgress to false
considering case
repeat with i from 1 to qdtiCount by 2 -- Parse odd-numbered items only.
set thisBit to item i of o's qdti
if ((count thisBit) > 0) or (i is qdtiCount) then
-- This is either a non-empty string or the last item in the list, so it doesn't
-- represent a quoted quote. Check if we've just been dealing with any.
if (quoteInProgress) then
-- All the parts of a quoted field containing quoted quotes have now been
-- passed over. Coerce them together using a quote delimiter.
set AppleScript's text item delimiters to "\""
set thisField to (items a thru (i - 1) of o's qdti) as string
-- Replace the reconstituted quoted quotes with literal quotes.
set AppleScript's text item delimiters to "\"\""
set thisField to thisField's text items
set AppleScript's text item delimiters to "\""
-- Store the field in the "current record" list and cancel the "quote in progress" flag.
set end of o's currentRecord to thisField as string
set quoteInProgress to false
else if (i > 1) then
-- The preceding, even-numbered item is a complete quoted field. Store it.
set end of o's currentRecord to item (i - 1) of o's qdti
end if


-- Now parse this item's field-separator-delimited text items, which are either non-quoted fields or stumps from the removal of quoted fields. Any that contain line breaks must be further split to end one record and start another. These could include multiple single-field records without field separators.
set o's possibleFields to getTextItems(thisBit, separator)
set possibleFieldCount to (count o's possibleFields)
repeat with j from 1 to possibleFieldCount
set thisField to item j of o's possibleFields
if ((count thisField each paragraph) > 1) then
-- This "field" contains one or more line endings. Split it at those points.
set theseFields to thisField's paragraphs
-- With each of these end-of-record fields except the last, complete the field list for the current record and initialise another. Omit the first "field" if it's just the stub from a preceding quoted field.
repeat with k from 1 to (count theseFields) - 1
set thisField to item k of theseFields
if ((k > 1) or (j > 1) or (i is 1) or ((count trim(thisField, true)) > 0)) then set end of o's currentRecord to trim(thisField, trimming)
set end of o's recordList to o's currentRecord
set o's currentRecord to {}
end repeat
-- With the last end-of-record "field", just complete the current field list if the field's not the stub from a following quoted field.
set thisField to end of theseFields
if ((j < possibleFieldCount) or ((count thisField) > 0)) then set end of o's currentRecord to trim(thisField, trimming)
else
-- This is a "field" not containing a line break. Insert it into the current field list if it's not just a stub from a preceding or following quoted field.
if (((j > 1) and ((j < possibleFieldCount) or (i is qdtiCount))) or ((j is 1) and (i is 1)) or ((count trim(thisField, true)) > 0)) then set end of o's currentRecord to trim(thisField, trimming)
end if
end repeat


-- Otherwise, this item IS an empty text representing a quoted quote.
else if (quoteInProgress) then
-- It's another quote in a field already identified as having one. Do nothing for now.
else if (i > 1) then
-- It's the first quoted quote in a quoted field. Note the index of the
-- preceding even-numbered item (the first part of the field) and flag "quote in
-- progress" so that the repeat idles past the remaining part(s) of the field.
set a to i - 1
set quoteInProgress to true
end if
end repeat
end considering


-- At the end of the repeat, store any remaining "current record".
if (o's currentRecord is not {}) then set end of o's recordList to o's currentRecord
set AppleScript's text item delimiters to astid


return o's recordList
end csvToList

-- Get the possibly more than 4000 text items from a text.
on getTextItems(txt, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set tiCount to (count txt's text items)
set textItems to {}
repeat with i from 1 to tiCount by 4000
set j to i + 3999
if (j > tiCount) then set j to tiCount
set textItems to textItems & text items i thru j of txt
end repeat
set AppleScript's text item delimiters to astid


return textItems
end getTextItems

-- Trim any leading or trailing spaces from a string.
on trim(txt, trimming)
if (trimming) then
repeat with i from 1 to (count txt) - 1
if (txt begins with space) then
set txt to text 2 thru -1 of txt
else
exit repeat
end if
end repeat
repeat with i from 1 to (count txt) - 1
if (txt ends with space) then
set txt to text 1 thru -2 of txt
else
exit repeat
end if
end repeat
if (txt is space) then set txt to ""
end if


return txt
end trim

set csvText to (read POSIX file totalName)
set myrecords to csvToList(csvText, {})
set mymax to count myrecords


tell application "Microsoft Excel"
activate
make new workbook
set newBook to make new workbook
set update remote references of newBook to true
activate object worksheet 1
tell worksheet 1 of active workbook
set font size of font object of row 1 to 14
set value of cell "A1" to ""


repeat with n from 1 to mymax


set cmycell to item n of myrecords
set aaa to item 1 of cmycell
set bbb to item 2 of cmycell
set value of cell aaa to bbb


end repeat


set weight of (get border of range "$A$1:$Q$1" which border edge bottom) to border weight medium


set color of interior object of range "$A$1:$Q$1" to {235, 249, 255}
end tell
end tell


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: Manipulating Excel from a sandboxed OSX app

Maarten Mostert-2
Thank you Karsten,

This doesn’t make things easier, but it is a very good article on this subject.

Regards,
@+Maarten



Le 11 mai 2015 à 13:28, Karsten Kusche <[hidden email]> a écrit :

Hi Maarten,

if i remember correctly, this may contain some answers to your questions: http://www.objc.io/issue-14/sandbox-scripting.html

Kind Regards
Karsten



-- 
Karsten Kusche - Dipl. Inf. (FH) - [hidden email]
Georg Heeg eK - Köthen
Handelsregister: Amtsgericht Dortmund A 12812 

Am 11. Mai 2015 bei 13:16:56, Maarten Mostert ([hidden email]) schrieb:

According to the Entitlement rules for Sandboxed OS X application it is possible to define scripting targets for your application.


The thing is that you cannot run an applescript with the osascript command as osascript is not entitled to run whatever script you want.
A solution (I thought) here is to compile your applescript into a standalone application and to sign it with the appropriate entitlements which appeared to work well for copying a cairo png image into the clipboard of Preview.

Obviously doing the same with Excel is a bit more complicated as you need to feed the compiled script with the data you want to put into Excel which is more work (see the script in the end).

Having done al that it still appears not to work, I get Sandbox collisions in the console and apple dev forums basically indicate me that this is allowed in only some rare circonstances like with mail and preview.

They indicate that the « official » way to use applescript from your application is to call it with NSAppleScript 

A full working code example stated to be able to run Excel from your app is given here:

- (NSString *) getStringFromAppleScript:(NSString *)theAppleScriptText {
     NSDictionary *errorDict= nil;
  NSAppleScript *appleScriptObject = [[NSAppleScript alloc] initWithSource:theAppleScriptText];
  NSAppleEventDescriptor *eventDescriptor = [appleScriptObject executeAndReturnError: &errorDict];
     if (([eventDescriptor descriptorType]) && (errorDict==nil)) {
     return [eventDescriptor stringValue];
  } else {
          return nil;
  }    

}

If there is anyone who knows how to implement the above snipet, please don’t hesitate to contact me.

Regards,

@+Maarten


For your information the applescript I used to read the csv tables for excel.

set aHome to "/Users"
set aUser to system attribute "USER"

set aFileName to "Library/Containers/com.stakepoint.portfolio/Data/Library/Application Support/StakePoint/tabledata.csv"
set totalName to aHome & "/" & aUser & "/" & aFileName
--display dialog totalName
--set theFileContents to (read POSIX file totalName)

(* Assumes that the CSV text adheres to the convention:
   Records are delimited by LFs or CRLFs (but CRs are also allowed here).
   The last record in the text may or may not be followed by an LF or CRLF (or CR).
   Fields in the same record are separated by commas (unless specified differently by parameter).
   The last field in a record must not be followed by a comma.
   Trailing or leading spaces in unquoted fields are not ignored (unless so specified by parameter).
   Fields containing quoted text are quoted in their entirety, any space outside them being ignored.
   Fields enclosed in double-quotes are to be taken verbatim, except for any included double-quote pairs, which are to be translated as double-quote characters.
       
   No other variations are currently supported. *)

on csvToList(csvText, implementation)
-- The 'implementation' parameter must be a record. Leave it empty ({}) for the default assumptions: ie. comma separator, leading and trailing spaces in unquoted fields not to be trimmed. Otherwise it can have a 'separator' property with a text value (eg. {separator:tab}) and/or a 'trimming' property with a boolean value ({trimming:true}).
set {separator:separator, trimming:trimming} to (implementation & {separator:",", trimming:false})

script o -- Lists for fast access.
property qdti : getTextItems(csvText, "\"")
property currentRecord : {}
property possibleFields : missing value
property recordList : {}
end script

-- o's qdti is a list of the CSV's text items, as delimited by double-quotes.
-- Assuming the convention mentioned above, the number of items is always odd.
-- Even-numbered items (if any) are quoted field values and don't need parsing.
-- Odd-numbered items are everything else. Empty strings in odd-numbered slots
-- (except at the beginning and end) indicate escaped quotes in quoted fields.

set astid to AppleScript's text item delimiters
set qdtiCount to (count o's qdti)
set quoteInProgress to false
considering case
repeat with i from 1 to qdtiCount by 2 -- Parse odd-numbered items only.
set thisBit to item i of o's qdti
if ((count thisBit) > 0) or (i is qdtiCount) then
-- This is either a non-empty string or the last item in the list, so it doesn't
-- represent a quoted quote. Check if we've just been dealing with any.
if (quoteInProgress) then
-- All the parts of a quoted field containing quoted quotes have now been
-- passed over. Coerce them together using a quote delimiter.
set AppleScript's text item delimiters to "\""
set thisField to (items a thru (i - 1) of o's qdti) as string
-- Replace the reconstituted quoted quotes with literal quotes.
set AppleScript's text item delimiters to "\"\""
set thisField to thisField's text items
set AppleScript's text item delimiters to "\""
-- Store the field in the "current record" list and cancel the "quote in progress" flag.
set end of o's currentRecord to thisField as string
set quoteInProgress to false
else if (i > 1) then
-- The preceding, even-numbered item is a complete quoted field. Store it.
set end of o's currentRecord to item (i - 1) of o's qdti
end if

-- Now parse this item's field-separator-delimited text items, which are either non-quoted fields or stumps from the removal of quoted fields. Any that contain line breaks must be further split to end one record and start another. These could include multiple single-field records without field separators.
set o's possibleFields to getTextItems(thisBit, separator)
set possibleFieldCount to (count o's possibleFields)
repeat with j from 1 to possibleFieldCount
set thisField to item j of o's possibleFields
if ((count thisField each paragraph) > 1) then
-- This "field" contains one or more line endings. Split it at those points.
set theseFields to thisField's paragraphs
-- With each of these end-of-record fields except the last, complete the field list for the current record and initialise another. Omit the first "field" if it's just the stub from a preceding quoted field.
repeat with k from 1 to (count theseFields) - 1
set thisField to item k of theseFields
if ((k > 1) or (j > 1) or (i is 1) or ((count trim(thisField, true)) > 0)) then set end of o's currentRecord to trim(thisField, trimming)
set end of o's recordList to o's currentRecord
set o's currentRecord to {}
end repeat
-- With the last end-of-record "field", just complete the current field list if the field's not the stub from a following quoted field.
set thisField to end of theseFields
if ((j < possibleFieldCount) or ((count thisField) > 0)) then set end of o's currentRecord to trim(thisField, trimming)
else
-- This is a "field" not containing a line break. Insert it into the current field list if it's not just a stub from a preceding or following quoted field.
if (((j > 1) and ((j < possibleFieldCount) or (i is qdtiCount))) or ((j is 1) and (i is 1)) or ((count trim(thisField, true)) > 0)) then set end of o's currentRecord to trim(thisField, trimming)
end if
end repeat

-- Otherwise, this item IS an empty text representing a quoted quote.
else if (quoteInProgress) then
-- It's another quote in a field already identified as having one. Do nothing for now.
else if (i > 1) then
-- It's the first quoted quote in a quoted field. Note the index of the
-- preceding even-numbered item (the first part of the field) and flag "quote in
-- progress" so that the repeat idles past the remaining part(s) of the field.
set a to i - 1
set quoteInProgress to true
end if
end repeat
end considering

-- At the end of the repeat, store any remaining "current record".
if (o's currentRecord is not {}) then set end of o's recordList to o's currentRecord
set AppleScript's text item delimiters to astid

return o's recordList
end csvToList

-- Get the possibly more than 4000 text items from a text.
on getTextItems(txt, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set tiCount to (count txt's text items)
set textItems to {}
repeat with i from 1 to tiCount by 4000
set j to i + 3999
if (j > tiCount) then set j to tiCount
set textItems to textItems & text items i thru j of txt
end repeat
set AppleScript's text item delimiters to astid

return textItems
end getTextItems

-- Trim any leading or trailing spaces from a string.
on trim(txt, trimming)
if (trimming) then
repeat with i from 1 to (count txt) - 1
if (txt begins with space) then
set txt to text 2 thru -1 of txt
else
exit repeat
end if
end repeat
repeat with i from 1 to (count txt) - 1
if (txt ends with space) then
set txt to text 1 thru -2 of txt
else
exit repeat
end if
end repeat
if (txt is space) then set txt to ""
end if

return txt
end trim

set csvText to (read POSIX file totalName)
set myrecords to csvToList(csvText, {})
set mymax to count myrecords


tell application "Microsoft Excel"
activate
make new workbook
set newBook to make new workbook
set update remote references of newBook to true
activate object worksheet 1
tell worksheet 1 of active workbook
set font size of font object of row 1 to 14
set value of cell "A1" to ""

repeat with n from 1 to mymax

set cmycell to item n of myrecords
set aaa to item 1 of cmycell
set bbb to item 2 of cmycell
set value of cell aaa to bbb

end repeat

set weight of (get border of range "$A$1:$Q$1" which border edge bottom) to border weight medium

set color of interior object of range "$A$1:$Q$1" to {235, 249, 255}
end tell
end tell


_______________________________________________ 
vwnc mailing list 
[hidden email] 
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc