One feller's views on the state of everyday computer science & its application (and now, OTHER STUFF) who isn't rich enough to shell out for www.myfreakinfirst-andlast-name.com
Using 89% of the same design the blog had in 2001.
FOR ENTERTAINMENT PURPOSES ONLY!!!
Back-up your data and, when you bike,
always wear white.
As an Amazon Associate, I earn from qualifying purchases. Affiliate links in green.
This stinks. Really. Say what you will about Microsoft, they know how to create and maintain a programming language. Whoever thought that s/vbscript/Applescript/gi would be a fair substitution in Office for Mac (even ignoring that it breaks legacy macros) was not a programmer.
Here's what my "final" code looks like. It's a mess and needs cleaning. But something that's INSANELY EASY in vbscript takes the below to do something similar. I couldn't even get it to know what cell format selections I'd made in Excel and ended up with an ugly kludge for zip codes and a hellah ugly kludge for phone numbers which were coming out in a format like "9.195553662E+9". What a mess.
I could clean it up some, taking out lesser kludges that aren't working, but I think you get the point better as is. Not intuitive. Not fully-featured. The stuff you have to roll yourself in Applescript (afaict) is insane and inane. Boy. Talk about not feeling fully featured.
Now I have to let it wade through over 3000 lines of Excel tonight...
1 on replaceText(find, replace, subject) 2 set prevTIDs to text item delimiters of AppleScript 3 set text item delimiters of AppleScript to find 4 set subject to text items of subject 5 6 set text item delimiters of AppleScript to replace 7 set subject to""& subject 8 set text item delimiters of AppleScript to prevTIDs 9 10 return subject 11 end replaceText 12 13 tell application "MicrosoftExcel" 14 15 --fromhttp://lists.apple.com/archives/applescript-users/2009/Jun/msg00331.html 16 set hc to find (row 1of used range of active sheet) what "PHONE" 17 set n tofirst column indexof hc 18 set f tofirst column indexof(column 1of used range of active sheet) 19 set col to column (n - f +1)of used range of active sheet 20 set number format of col to"0" 21 --doesn'tseemtowork 22 23 24 --say"Savingwitheveryrow" 25 26 set lastCol to10--yes,I'mcheating. 27 --youhavetoknowthelastcolandrowforthistowork 28 set lastRow to100--IsupposeIcouldadddialogboxestoaskmaxnums 29 set c to1 30 set r to90--that'sthefirstrowthat'sgotinfoitin;skipthefirstrow'scolheaders 31 set strCmd to"" 32 33 try 34 repeatuntil r = lastRow +1 35 set strCmd to strCmd &"INSERTINTOrestaurants(phone,company,address,¬ 36 city, state, zip, owner, gm, agent, chef)" 37 set strCmd to strCmd &"VALUES(" 38 set daComma to"" 39 40 --'','','',42,,'','','')" 41 42 set c to1 43 44 repeatuntil c = lastCol +1 45 --displaydialog"Row:"&r&"Col:"&c 46 47 set theValue toget value of cell c of row r of active sheet 48 49 if(c =1)then--thenwehaveaphonenumber 50 --ExcelviaApplescriptisborkingnumberslikemad. 51 --ThereareRightWaystohandlethis.They 52 --don'tseetobeworkingwell(seeabove). 53 --we'regoingtocheatlikemadinstead. 54 --TODO:KLUDGE-A-RIFFIC 55 if(text 11of(theValue as string)="E")then 56 set strOut to text 1of(theValue as string) ¬ 57 & text 3thru10of(theValue as string)&"0" 58 else 59 set strOut to text 1of(theValue as string) ¬ 60 & text 3thru11of(theValue as string) 61 endif 62 63 set strCmd to strCmd & daComma & strOut 64 set daComma to"," 65 66 elseif(c =6)then--thenwehaveazipcode 67 --can'tusethisbranchforphonenumbers 68 --astheyevaluatetoanumberlargerthan 69 --ourlargestpossibleinteger 70 if(countof characters of(theValue as string))=5then 71 set theValue to theValue as integer 72 endif 73 set strCmd to strCmd & daComma &(theValue as string) 74 set daComma to"," 75 elseif(c =5)then 76 set strCmd to strCmd & daComma &"42" 77 --hardcodingSCtosavealookup 78 set daComma to"," 79 else--elseit'sastring 80 if theValue =""then 81 set theValue to"empty" 82 endif 83 set strCmd to strCmd & daComma &"'"&(theValue as string)&"'" 84 set daComma to"," 85 endif 86 87 set c to(c +1) 88 endrepeat 89 set strCmd to strCmd &")"&return--returnaddsaMacOSnewline 90 set r to(r +1) 91 if r mod50=0then 92 --beep--addsomesortoffeedback. 93 say r --addcoolerfeedback.Thistalks. 94 --Btw,ApplescriptforthisinExcelisinsanelyslooooow 95 --vs.myexperiencewithvbs.Ittakesseveralsecondsto 96 --do10rowsoftencolumnsofcustomeraddressandphone,etc. 97 --information. 98 endif 99 endrepeat 100 101 set theFilePath to(path to desktop as string)&"test11.txt"as string 102 set theFileReference toopen for access theFilePath with write permission 103 write strCmd to theFileReference 104 close access theFileReference 105 106 onerror errMessage number errNum from errSource partial result errList ¬ 107 to class_constant --variousvariablesstoreinformationabouttheerror 108 set bigmessage to"Theerroris:"& errMessage &return ¬ 109 &"Thenumberis:"& errNum &return&"Thesourceis:"& errSource &return ¬ 110 &"r&c:"& r &"&"& c 111 error bigmessage --errorstatementdisplaysdialogboxtouser 112 say "errorintheloop" 113 close access theFileReference 114 --thiscloseseemsimportant.Ican'ttellhowtoget 115 --backthehandletoclosethisifIhadanerrorcauseit 116 --tostayopenfromapastscriptinvocation. 117 endtry 118 119 say "Done!" 120 --displaydialog"Donewithwritetofile" 121 endtell
(Edit: See this on tells. I had to tweak the above a good bit for it to work well, but it still serves as a pretty decent low-investment Applescript (vs. VBA) code snippet. It's an order of magnitude more complicated.)
The postings on this site are [usually] my own and do not necessarily reflect the views of any employer, past or present, or other entity. About Our Author