Why are string tables split into sections in an .rc file? - windows

In an .rc file strings are grouped by sections of at most 16 strings.
So in a typical .rc file we usually have something like this:
...
STRINGTABLE // section 1
BEGIN
IDS_SOMEID_1 "Some text 1"
IDS_SOMEID_2 "Some text 1"
IDS_SOMEID_3 "Some text 3"
END
STRINGTABLE // section 2
BEGIN
IDS_SOMEID_4 "Some text 4"
IDS_SOMEID_5 "Some text 5"
IDS_SOMEID_6 "Some text 6"
END
...
and the IDs of the strings in one section only differ by the least 4 bits.
I wonder why these sections need to be specified explicitely in the .rc file. The resource compiler could take care of this entirely so we could have one single stringtable per .rc file section like this:
STRINGTABLE
BEGIN
IDS_SOMEID_1 "Some text 1"
IDS_SOMEID_2 "Some text 1"
IDS_SOMEID_3 "Some text 3"
IDS_SOMEID_4 "Some text 4"
IDS_SOMEID_5 "Some text 5"
IDS_SOMEID_6 "Some text 6"
END
Does anyone have a rational explanation?
I found some (insufficient) explanations here:
STRINGTABLE resource
MS knowledge base Q196774 (link doesn't work anymore)

Related

How to update AppleScript List "Forever"

Look at the following code:
set TheStringsQ1Happy to {"Fabulous", "Great", "Alright", "Excited", "Not Bad", "", "Decent", "Fine", "Awesome", "Bored", "Cool", "Sad", "Fantastic", "Alright", "Good", "Ok"}
set theResponse to the text returned of (display dialog "" default answer "" giving up after 5)
if TheStringsQ1Happy contains theResponse then
display dialog "That's Great!"
else
say "That term is not in my vocabulary. Would you like me to add it?" using "Tom" speaking rate 220
set theResponseNotInVocabulary to text returned of (display dialog "" default answer "" giving up after 5)
if theResponseNotInVocabulary is "Yes" then
set end of TheStringsQ1Happy to theResponse
return TheStringsQ1Happy
end if
Although I can update TheStringsQ1Happy, this update only lasts the span of the script. How can I change the code so that every time I run the script, it also contains the updated vocabulary?
For example, if I said "All Good", the computer would recognize that the vocabulary is not on the list, and would later update this list only for that instance. How can I make it so that "All Good" stays for every instance from now on?
The following is strictly an example to help you with what you asked, not fix the broken code you posted.
If you run the following in Script Editor:
property theList : {1, 2, 3}
copy (count theList) + 1 to end of theList
log theList
You'll see theList as a property grow by 1 each time you run it, that is until the script is recompiled.
If you need absolute long term storage where nothing will be lost of anything added to theList, then you need to save to and retrieve from a disk file.
Variables in AppleScript don't span outside the duration of execution of the script in which they are defined, as you've quite rightly noticed.
However, a property can, and will continue into subsequent executions of a script with the information left over from the previous execution.
Bear in mind, though, that a property will get reset (restored to its original value) each time the script is re-compiled (which happens whenever you make edits to it, or trigger it to compile manually).
With this in mind, change this line:
set TheStringsQ1Happy to {"Fabulous", "Great", "Alright", "Excited", "Not Bad", "", "Decent", "Fine", "Awesome", "Bored", "Cool", "Sad", "Fantastic", "Alright", "Good", "Ok"}
to this:
property TheStringsQ1Happy : {"Fabulous", "Great", "Alright", "Excited", "Not Bad", "", "Decent", "Fine", "Awesome", "Bored", "Cool", "Sad", "Fantastic", "Alright", "Good", "Ok"}
and you'll be good to go.
If you want a more permanent way to ensure you don't accidentally lose the new additions to this property, such as when you need to make any changes to the script in the future that will reset its value, then you'll need to store the information in an external file, which will serve as a sort of "dictionary" of vocabulary terms.
The simplest way to do this is to create a text file and put each item of the list on its own line, like this:
Fabulous
Great
Alright
Excited
...etc.
Save it as something like HappyTerms.txt, somewhere like your Documents folder, then change the variable declaration for TheStringsQ1Happy to this:
set TheStringsQ1Happy to the paragraphs of (read "/Users/%you%/Documents/HappyTerms.txt")
replacing %you% with the name of your Home folder in which your Documents folder lives. In fact, it's a useful idea to put the path to this text file in its own variable definition just beforehand:
set VocabDictionary to "/Users/%you%/Documents/HappyTerms.txt"
set TheStringsQ1Happy to the paragraphs of (read VocabDictionary)
Finally, to make changes to this file and add new terms to it, immediately after this line:
if theResponseNotInVocabulary is "Yes" then set end of TheStringsQ1Happy to theResponse
simply add either these lines:
set AppleScript's text item delimiters to return
write (TheStringsQ1Happy as text) to VocabDictionary starting at 1
OR this line
write "\n" & theResponse to VocabDictionary starting at (get eof VocabDictionary) + 1
The first version overwrites the entire file with all the terms in the new list. The second version simply appends the new addition to the end of the file. You might want to experiment with both and get the file turning out the way you want it to, as you'll sneakily discover that one might give you a stray blank line in the file that results harmlessly in an empty string "" appearing in your list; whilst the other does not; but I'll leave you to figure out if and why this happens, and—should you really want it not to happen—how to prevent it. 🙃 Either way, it shouldn't cause you any problems.
If you have any other queries, post a comment and I'll back to you. If this is helpful, don't forget to +1 my answer, and mark it as "correct" if solves your problem for you.
This works for me using the latest version of Sierra
If this script is saved as an application,and you launch this new app... every time a new Item is added to the list, that new item will remain in the list every time you reopen the application. However if you open the application again in script editor,to edit the code and re-save... You will lose all of the values of the added list items And the whole cycle starts over again with the default list.
property TheStringsQ1Happy : {"Fabulous", "Great", "Alright", "Excited", "Not Bad", "", "Decent", "Fine", "Awesome", "Bored", "Cool", "Sad", "Fantastic", "Alright", "Good", "Ok"}
set theResponse to (display dialog "How Do You Feel Today?" default answer "" giving up after 25)
if text returned of theResponse is in TheStringsQ1Happy then
display dialog "That's Great!"
else
say "That term is not in my vocabulary. Would you like me to add it?" using "Tom" speaking rate 220
set theResponseNotInVocabulary to display dialog "Add This Term" & " " & quote & text returned of theResponse & quote & " " & "To My Vocabulary?" buttons {"No", "Yes"} default button 2
if the button returned of the result is "Yes" then
if TheStringsQ1Happy does not contain text returned of theResponse then
set end of TheStringsQ1Happy to text returned of theResponse
end if
return TheStringsQ1Happy
end if
end if

ZPL: how to set max width of a "text field"

Given a list of small strings (1 to 3 words each), I would like to print them in 2 columns using ZPL for Zebra Printers. For example, if the list is ["A", "B", "C", "D", "E"], I would like my label to look like this:
A B
C D
E
However, if strings are a little bit longer, I would like to be able to truncate them so that columns don't overlap. For example, if the list is ["string 1", "string 2", "long string 3", "string 4", "string 5"], the label should look like this:
string 1 string 2
long str string 4
string 5
I see 2 possible approaches to this:
1) Using some ZPL command that I have not been able to find yet
2) Calculating the width of the strings in pixels. In this case I would need to know what is the font used by ZPL.
I'm using this command for text printing:
^A0,N,30,30
^FDtext^FS
It looks like ^TB is the solution:
^A0N,30,30
^TBN,250,29
^FDtext should go here^FS

Can a whose clause be used to filter text element lists such as words, characters and paragraphs

I have the following working example AppleScript snippet:
set str to "This is a string"
set outlist to {}
repeat with wrd in words of str
if wrd contains "is" then set end of outlist to wrd
end repeat
I know the whose clause in AppleScript can often be used to replace repeat loops such as this to significant performance gain. However in the case of text element lists such as words, characters and paragraphs I haven't been able to figure out a way to make this work.
I have tried:
set outlist to words of str whose text contains "is"
This fails with:
error "Can’t get {\"This\", \"is\", \"a\", \"string\"} whose text contains \"is\"." number -1728
, presumably because "text" is not a property of the text class. Looking at the AppleScript Reference for the text class, I see that "quoted form" is a property of the text class, so I half expected this to work:
set outlist to words of str whose quoted form contains "is"
But this also fails, with:
error "Can’t get {\"This\", \"is\", \"a\", \"string\"} whose quoted form contains \"is\"." number -1728
Is there any way to replace such a repeat loop with a whose clause in AppleScript?
From page 534 (working with text) of AppleScript 1-2-3
AppleScript does not consider paragraphs, words, and characters to be
scriptable objects that can be located by using the values of their
properties or elements in searches using a filter reference, or whose
clause.
Here is another approach:
set str to "This is a string"
set outlist to paragraphs of (do shell script "grep -o '\\w*is\\w*' <<< " & quoted form of str)
As #adayzdone has shown. It looks like you are out of luck with that.
But you could try using the offset command like this.
set wrd to "I am here"
set outlist to {}
set str to " This is a word"
if ((offset of space & "is" & space in str) as integer) is greater than 0 then set end of outlist to wrd
Note the spaces around "is" . This makes sure Offset is finding a whole word. Offset will find the first matching "is" in "This" otherwise.
UPDATE.
To use it as the OP wants
set wrd to "I am here"
set outlist to {}
set str to " This is a word"
repeat with wrd in words of str
if ((offset of "is" in wrd) as integer) is greater than 0 then set end of outlist to (wrd as string)
end repeat
-->{"This", "is"}

ruby regex to extract two parts: digits, then whatever comes after

My user input is a string I need to split into two parts, (1) a partial phone number [any sequence of digits - . space, parens so I assume that is represented by /[\d\. \-\(\)]/ ] and (2) whatever follows (if anything).
For example
"88 comment" -> "88" & "comment"
"415-915 second part" --> "415-915" & "second part"
"(415) 915 part 2" --> "(415) 915" & "part 2"
"a note" --> "" & "a note"
"part 2" --> "" & "part 2"
As a relative newbie to ruby and regex, I have no idea how to extract multiple parts, and how to define the second part as being whatever comes after the first part (which basically means whatever comes after anything that doesn't match the first part)
Here's the regex (I'll explain below):
/^([-\d. ()]*)(.*)$/
^ means "start at the beginning of the string"
In ([-\d. ()]*), the * means "match any number of the previous character, and the parens mean to create a match group (this is how you will get the value later). So this is the first sequence.
In (.*), . means "match any single character", so .* means "match any number of any characters", it's basically a catch-all. The parens create a second match group.
$ means "finish at the end of the string"
So in ruby:
string =~ /^([-\d. ()]*)(.*)$/
puts $1.strip # is the phone number (with excess whitespace removed)
puts $2.strip # is the rest (with excess whitespace removed)
Try /([\d.\s()/-]*)(.+)/ The first group will capture the number, the second one the "other" part. I don't know ruby, so you have to implement that pattern yourself.

Getting line by line in Apple Script from Address Books Note Field

I have two lines in my address book's note field
Test 1
Test 2
I would like to get each line as a separate value or get the last line from the notes field.
I tried doing it this way:
tell application "Address Book"
set AppleScript's text item delimiters to "space"
get the note of person in group "Test Group"
end tell
but the result is
{"Test 1
Test 2"}
I'm looking for :
{"Test1","Test2"}
What am I doing incorrect?
There are a few things wrong with your code. First, you never actually ask for the text items of the note :-) You just get the raw string. The second is that set AppleScript's text item delimiters to "space" sets the text item delimiters to the literal string space. Thus, for instance, running
set AppleScript's text item delimiters to "space"
return text items of "THISspaceISspaceAspaceSTRING"
returns
{"THIS", "IS", "A", "STRING"}
Secondly, even if you had " " instead of "space", this would split your string on spaces, and not newlines. For instance, running
set AppleScript's text item delimiters to " "
return text items of "This is a string
which is on two lines."
returns
{"This", "is", "a", "string
which", "is", "on", "two", "lines."}
As you can see, "string\nwhich" is a single list item.
To do what you want, you can just use paragraphs of STRING; for instance, running
return paragraphs of "This is a string
which is on two lines."
returns the desired
{"This is a string", "which is on two lines."}
Now, I'm not entirely clear on exactly what you want to do. If you want to get this for a specific person, you can write
tell application "Address Book"
set n to the note of the first person whose name is "Antal S-Z"
return paragraphs of n
end tell
You have to split it into two statements because, I think, paragraphs of ... is a command, whereas everything on the first line is a property access. (I usually discover these things via trial and error, to be honest.)
If, on the other hand, you want to get this list for every person in a group, it's slightly harder. One big problem is that people without a note get missing value for their note, which isn't a string. If you want to ignore these people, then the following loop will work
tell application "Address Book"
set ns to {}
repeat with p in ¬
(every person in group "Test Group" whose note is not missing value)
set ns to ns & {paragraphs of (note of p as string)}
end repeat
return ns
end tell
The every person ... bit does exactly what it says, getting the relevant people; we then extract their note's paragraphs (after reminding AppleScript that the note of p really is a string). After this, ns will contain something like {{"Test 1", "Test 2"}, {"Test 3", "Test 4"}}.

Resources