Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Simple Color Counter App... stuck!

I'm trying to create an app using applescript that I can open quickly, double click the color of choice and be done until the next time I need it.


The app should increment the Color I choose by 1 and close.


Here is what I have so far:

User uploaded file

But I can't seem to get it to work the way I want. I want it to load from a txt file and then save the updated results to the same txt file.


Here is the code I have so far:

set theFile to "/Users/me/Desktop/ColorData.txt"

set colorList to paragraphs of (read POSIX file theFile)

set selectedColor to choose from list colorList with title "Color Counter" with prompt "" OK button name "+1" cancel button name "Close"


The code after this just messes everything up... so I didn't include it. If anyone can help me find a solution to this, it would be appreciated. I've decided not to use datetimestamps with this since it seems to add another level of difficulty. But it would be a bonus to be able to have the last updated row moved to the bottom.


Thanks!

Posted on Mar 24, 2015 10:58 PM

Reply
6 replies

Mar 25, 2015 7:17 AM in response to notabill

The following script might do what you are asking for:


set theFileName to "ColorData.txt"

set theFile to ((path todesktopastext) & theFileName) as alias

set theText to readtheFile

set theColorList to paragraphs of theText


set theList to choose from listtheColorListwith title "Color Counter" with prompt "" OK button name "+1" cancel button name "Close"

if result is false then return

set theSelectedColor to item 1 of theList


-- Increment the selected color by 1:

set P to offsetof "." intheSelectedColor

set theNumber to (text (P + 1) through -1 of theSelectedColor) as integer

set theUpdatedColor to (text 1 through P of theSelectedColor) & (1 + theNumber) as text


-- Get the index of the selected color:

set N to counttheColorList

repeat with k from 1 to N

if itemk of theColorList is theSelectedColor then exit repeat

end repeat


-- Move the updated item to the end of the list:

if k = 1 then

set theNewColorList to the rest of theColorList & theUpdatedColor

else if k = N then

set theNewColorList to theColorList

set itemk of theNewColorList to theUpdatedColor

else

set theNewColorList to items 1 through (k - 1) of theColorList ¬

& items (k + 1) through -1 of theColorList & theUpdatedColor

end if


-- Convert the list back to paragraphs:

set theNewText to ""

repeat with k from 1 to N

set theNewText to theNewText & itemk of theNewColorList & return

end repeat

set theNewText to text 1 through -2 of theNewText


-- Update the text file:

set fRef to open for accesstheFile with write permission

set eoffRefto 0

writetheNewTexttofRef

close accessfRef

Mar 25, 2015 10:55 AM in response to notabill

Hello


Another handy method is to use plist to store data. Like this:


_main() on _main() script o property |PLIST| : (path to desktop)'s POSIX path & "ColorData.plist" property |INITIAL_DATA| : {|Blue|:0, |Yellow|:0, |Red|:0, |Black|:0, |Green|:0} property |FS| : "." property kk : {} property vv : {} property aa : {} tell application "System Events" if not (exists property list file |PLIST|) then tell (make new property list file with properties {name:|PLIST|}) set value to |INITIAL_DATA| end tell end if tell property list file |PLIST| tell property list items set {kk, vv} to {name, value} end tell end tell end tell repeat with i from 1 to count my kk set my aa's end to my kk's item i & |FS| & my vv's item i end repeat set r to choose from list aa with title "Color Counter" OK button name "+1" if r = false then error number -128 -- user cancel set r to r's item 1 set k to r's text 1 thru ((offset of |FS| in r) - 1) tell application "System Events" tell property list file |PLIST| tell property list item k set value to value + 1 end tell end tell end tell end script tell o to run end _main



Good luck,

H

Mar 25, 2015 11:38 AM in response to notabill

Hello


Here's a revised version to store time stamp of update per colour as well and present colour list in ascending order of the time stamp.



_main() on _main() script o property |PLIST| : (path to desktop)'s POSIX path & "ColorData_2.plist" property |TS| : current date property |INITIAL_DATA| : {|Blue|:{0, |TS|}, |Yellow|:{0, |TS|}, |Red|:{0, |TS|}, |Black|:{0, |TS|}, |Green|:{0, |TS|}} property |FS| : "." property kk : {} property vv : {} property aa : {} tell application "System Events" if not (exists property list file |PLIST|) then tell (make new property list file with properties {name:|PLIST|}) set value to |INITIAL_DATA| end tell end if tell property list file |PLIST| tell property list items set {kk, vv} to {name, value} end tell end tell end tell repeat with i from 1 to count my kk -- {key & |FS| & counter, timestamp} set my aa's end to {my kk's item i & |FS| & my vv's item i's item 1, my vv's item i's item 2} end repeat inssort(aa, my cmp) repeat with a in (a reference to my aa) set a's contents to a's item 1 end repeat set r to choose from list aa with title "Color Counter" OK button name "+1" if r = false then error number -128 -- user cancel set r to r's item 1 set k to r's text 1 thru ((offset of |FS| in r) - 1) tell application "System Events" tell property list file |PLIST| tell property list item k set {c, t} to value set value to {c + 1, current date} -- {counter, timestamp} end tell end tell end tell end script tell o to run end _main on cmp(x, y) -- comparator (sort in ascending order by item 2) return x's item 2 > y's item 2 end cmp on inssort(aa, cmp_) (* list aa : target list to be sorted in place handler cmp_ : comparator * cmp_(x, y) must return true iff list element x and y are out of order. *) script o (* Insertion sort handler (inserting from right to left) giving list sorted in place *) property xx : aa property cmp : cmp_ on _cmp_(x, y) -- primary comparator return x > y end _cmp_ if cmp_ = {} then set my cmp to _cmp_ -- comparator fallback local ti, t, k repeat with i from 2 to (count my xx) set ti to my xx's item i set k to i repeat while k > 1 set t to my xx's item (k - 1) if my cmp(t, ti) then set my xx's item k to t set k to k - 1 else exit repeat end if end repeat if k < i then set my xx's item k to ti end repeat end script tell o to run end inssort



Regards,

H

Mar 25, 2015 1:47 PM in response to notabill

Thanks for your quick responses! All very helpful. I tried to use the plist at one point but gave up on it... so it's good to know that it can work. I'm going with Pierre's solution since it has a little less code and it's easier for me to follow. Thanks Pierre!


I was also toying with python and php for this project... but I think this is the best option considering the alternatives.

Simple Color Counter App... stuck!

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.