Issue setting returned text to variable within repeat loop - OS X 10.9.x - variables

The goal of this script is to:
Ask the user how many text replacement (System Preferences>Keyboard>Text) shortcuts they'd like to have. The text returned is set to my variable "gTextReplacementNum" as number. See my second handler "HowMany()"
Have the user provide the text replacement shortcut and the text to do the replacing for the number of shortcuts they wanted. See my third handler "GetText()"
Take the user provided text contained in a variable to create a new AppleScript doc that does all the heavy lifting for them. Code not yet written; not within scope of question.
Then they have a personalized AppleScript Application Bundle they may launch on their Mac to auto-populate the text replacement preferences pane.
I am having trouble getting this to work properly. I need the loop to keep adding the answers to a variable as a list or to a variable that increments its name according to the loop instance (e.g. TextReturned_i, TextReturned_i+1, etc).
Have I adequately explain this?
global gTextReplacementNum
set gTextReplacementNum to 0
# Main Logic Begins
try
Start()
HowMany()
GetText()
on error errText number errNum
display alert "Error " & errNum message errText
end try
# Main Logic Ends
# Handlers Begin
-- First Handler
on Start()
display alert "Automated Text Replacement v1.0" message "Created by: Me
myemail#domain.com" buttons {} giving up after 4
display alert "About" message "This app will have you provide a text 'short cut' to replace with and replacement text. It then compiles all this into an application that can be run on any Mac.
Would you like to continue?" buttons {"No", "Yes"} cancel button 1 default button 2
end Start
-- Second Handler
on HowMany()
display dialog "How many text replacement shortcuts would you like?
Please enter numericals only. (1, 2, 3)" default answer "" buttons {"Cancel", "Okay"} default button 2 cancel button 1
copy the result as list to {ButtonPressed, TextReturned}
set gTextReplacementNum to TextReturned as number
end HowMany
-- Third Handler
on GetText()
repeat with i from 1 to gTextReplacementNum as number
display dialog "What text would you like to replace?
(this is your shortcut)" default answer "" buttons {"Cancel", "Okay"} default button 2 cancel button 1
set TextShortcut to text returned of result as list
display dialog "What is the replacement text?
(this is what the shortcut fills out)" default answer "" buttons {"Cancel", "Okay"} default button 2 cancel button 1
set TextReplaced to text returned of result as list
end repeat
end GetText
# Handlers End

In your GetText() handler, you are replacing the value TextShortcut and TextReplaced each time. You need to
set aList to aList & newValue
to build a list in a repeat loop.
Also, as is, this handler never returns the value of these two lists. So, I'd suggest, using your scheme, make these two variables globals as well.
So, the full changes are:
1. Add to the declarations:
global gTextReplacementNum
global gTextShortcut
global gTextReplaced
set gTextReplacementNum to 0
set gTextShortcut to {}
set gTextReplaced to {}
and 2. edit your GetText() handler:
-- Third Handler
on GetText()
repeat with i from 1 to gTextReplacementNum as number
display dialog "What text would you like to replace?
(this is your shortcut)" default answer "" buttons {"Cancel", "Okay"} default button 2 cancel button 1
set gTextShortcut to gTextShortcut & (text returned of result)
display dialog "What is the replacement text?
(this is what the shortcut fills out)" default answer "" buttons {"Cancel", "Okay"} default button 2 cancel button 1
set gTextReplaced to gTextReplaced & (text returned of result)
end repeat
end GetText
An alternate method would be to read a tab delim file and work from that with a standard script. Something like:
property fileName : "shortcuts.txt"
set filePath to (path to desktop as string) & fileName
set theData to read file filePath
set theRecords to paragraphs of theData
set oldDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to tab
repeat with thisPair in theRecords
set {theShortcut, theReplacement} to text items of thisPair
setKeyboardPref(theShortcut, theReplacement)
end repeat
set AppleScript's text item delimiters to oldDelim
on setKeyboardPref(theShortcut, theReplacement)
-- set up the pair
display dialog theShortcut & return & theReplacement
end setKeyboardPref

Related

How to use multiple buttons to populate text box with text string in MS Access Form?

I am building a form in MS Access and I am trying to use buttons to populate a sequence of text into a text box. The idea is that each button will add onto the information to the string sequence. So far I have only been able to populate the text box with the info of a single button, which gets replaced when another button is clicked.
I used the following for each button:
Private Sub btnBP_Click()
txtSequence.SetFocus
txtSequence.Text = "BP"
How can I make subsequent button clicks add onto the information and not replace it?
Don't replace the text, append the new text:
Private Sub btnBP_Click()
Me!txtSequence.Value = Me!txtSequence.Value & "BP"
End Sub

Apple Script:Find and replace not working

everybody, I am trying to make the user enter the name of the movie they want subtitles for and the for apple script to automatically search the subtitle website for the name of the movie they inputted.
To do this all of the spaces in the movie name need to be replaced to the + sign because urls convert spaces to the + sign. The code is not working an im getting the following errors:
Expected “end” but found “on”.
A “(” can’t go after this identifier.
Here is my code;
on run
display dialog "What's the name of the movie?" default answer " " with title "What's the name of the movie?" buttons {"OK"} default button 1
set moviename to text returned of the result
set theText to moviename
set theSearchString to " "
set theReplacmentString to "+"
end findAndReplaceInText(theText, theSearchString, theReplacementString)
set AppleScript's text item delimiters to theSearchString
set theTextItems to every text item of theText
set AppleScript's text item delimiters to theReplacementString
set theText to theTextItems as string
set AppleScript's text item delimiters to ""
return theText
end findAndReplaceInText
goToWebPage("https://rs.titlovi.com/prevodi/?prevod= & thetext")
tell application "Safari"
activate
set URL of document 1 to theWebPage
end tell
end goToWebPage
end run
Thank you in advance.
Functions (handlers, in AppleScript speak) may not be nested in AppleScript. So you need to either move findAndReplaceInText and goToWebPage outside of on run, or merge their functionality into on run without using handlers.
Handlers begin with on handlerName and end with end handlerName; you have findAndReplaceInText beginning and ending with end findAndReplaceInText.
Here’s how it might work after separating the handlers:
on run
display dialog "What's the name of the movie?" default answer " " with title "What's the name of the movie?" buttons {"OK"} default button 1
set moviename to text returned of the result
set moviename to findAndReplaceInText(moviename, " ", "+")
goToWebPage("https://rs.titlovi.com/prevodi/?prevod=" & moviename)
end run
on findAndReplaceInText(thetext, theSearchString, theReplacementString)
set AppleScript's text item delimiters to theSearchString
set theTextItems to every text item of thetext
set AppleScript's text item delimiters to theReplacementString
set thetext to theTextItems as string
set AppleScript's text item delimiters to ""
return thetext
end findAndReplaceInText
on goToWebPage(theWebPage)
tell application "Safari"
activate
set URL of document 1 to theWebPage
end tell
end goToWebPage
I’ve verified this code in Safari on Mac OS X 10.14.6.

DocumentBeforePrint does not fire for Envelope print for VBA in Word

I am trying to switch users to use a different printer for envelopes in Word. If they create the envelope, then print it, it works great using DocumentBeforePrint. However, this event is NOT fired when using the Print button on the dialog Mailings --> Envelopes. Is there any event fired when this happens that I can catch?
thanks,
Mike
There is no event, as such, however...
It is possible to Display, Execute or Show Word's built-in dialog boxes. A number of the controls in these dialog boxes are exposed so that they can be set or read. And the button used to dismiss the dialog box returns a value that can be evaluated.
The list of exposed controls is documented here. The WdWordDialog enumerator for envelopes is wdDialogToolsCreateEnvelope. The properties listed are for both envelopes and labels, keep that in mind when sorting through the possibilities. Note that there is no IntelliSense for these properties. (For .NET people reading this, the properties are accessed via late-binding, meaning C# must use PInvoke in order to work with them.)
To read the user's input, place the properties after the method; to make "default setting", place the properties before the method.
Dismissing this dialog box returns the following values:
0 Cancel (or the "X" button)
1 Print
2 Add to Document:
Since you need to do something before the print job is sent, you probably need to use Display rather than Show. Display does not execute the dialog box when the user dismisses it. Instead, it's necessary to capture the settings, do something with them, then Execute the the dialog box.
For example, the following code displays the dialog box to the user, capture's the delivery address typed into that box, then handles the various return values.
Sub PrintEnvelopes()
Dim dlg As Word.Dialog
Dim retVal As Long
Dim recipAddress As String
Set dlg = Application.Dialogs(wdDialogToolsCreateEnvelope)
With dlg
retVal = .Display
recipAddress = .envaddress
End With
Select Case retVal
Case 1 'Print
With dlg
'Change the printer here
.envaddress = recipAddress
.Execute
End With
Case 0 'Cancel
Case 2 'Add to document
With dlg
.envaddress = recipAddress
.Execute
End With
End Select
End Sub
Turns out, there are events you can place in a module to intercept the Envelope tool launch (h/t http://www.gmayor.com/fax_from_word.htm). As such, I added the following to one of my modules, and it runs when Mailings-->Envelopes is selected, so I can switch the printer, load the dialog, then switch the printer back after the dialog is finished:
Sub ToolsCreateEnvelope()
Dim DoChangePrinter As Boolean
Dim OriginalPrinterName As String
DoChangePrinter = False
OriginalPrinterName = Application.ActivePrinter
CurrentPrinterName = OriginalPrinterName
//Change to use color if on B&W printer
If InStr(1, LCase(CurrentPrinterName), "b&w") Then
CurrentPrinterName = Replace(CurrentPrinterName, "B&W", "COLOR")
DoChangePrinter = True
End If
If (DoChangePrinter) Then ChangePrinter
Application.ActiveDocument.Envelope.DefaultOmitReturnAddress = True
//Show dialog
Dim oDlg As Dialog
Set oDlg = Dialogs(wdDialogToolsCreateEnvelope)
With oDlg 'Pop up the envelopes dialog
.extractaddress = True
.Show
End With
ActivePrinter = OriginalPrinterName 'Restore the original printer
End Sub

AppleScript : Search safari tab and open tab

I would like to find a safari tab, and focus on this tab
This is not working, however the tab is found alright, I just can't open this tab.
tell application "Safari"
repeat with t in tabs of windows
tell t
if name starts with "facebook" then open tab
end tell
end repeat
end tell
Also I wonder can I found some text from a another tab without focus on this tab ?
to getInputByClass75(theClass, num)
tell application "Safari"
set input to do JavaScript "
document.getElementsByClassName('" & theClass & "')[" & num & "].innerHTML;" in document 1
end tell
return input
end getInputByClass75
getInputByClass75("Order", 0)
set theText to Unicode text
set theSource to getInputByClass75("Order", 0)
property leftEdge75 : "<a class=\"columns\" href=\"/Web"
property rightEdge75 : "\">Display</a>"
set saveTID to text item delimiters
set text item delimiters to leftEdge75
set classValue to text item 2 of theSource
set text item delimiters to rightEdge75
set OrderLink to text item 1 of classValue
set text item delimiters to saveTID
OrderLink
The right phrase to bring a tab to foreground is
tell window x to set current tab to tab y
Try this
tell application "Safari"
repeat with w in windows
if name of w is not "" then --in case of zombie windows
repeat with t in tabs of w
if name of t starts with "facebook" then
tell w to set current tab to t
set index of w to 1
end if
end repeat
end if
end repeat
end tell
--
You can execute javascripts in background tabs. Specify the tab like this:
tell application "Safari"
do JavaScript "document....." in tab 1 of window 1
end tell

How can I allow editing the value in the combolist of C1FlexGrid in vb10?

How to allow editing the value in the combolist of C1FlexGrid in vb10?
Is it not okay to type the value in the combolist of c1 flex grid?
I am using the following snippet
GridComboTransferStr = dbRow("RSPrice1") & "|" & Trim(GridComboTransferStr) & ""
ComboList in C1FlexGrid in a ComponentOne Studio for Winforms works analogous to ComboList in DatGridView of MS Controls. ComboList can be editable in C1FlexGrig in VB10. In fact it is an important and widely used functionality of C1FlexGrid.
The ComboList property of C1FlexGrid enables this functionality. It specifies the type of editor to be used when editing a cell. You may use a text box, drop-down list, drop-down combo, or an edit button to pop up custom editor forms.
To use the ComboList property, set the AllowEditing property to true and respond to the BeforeEdit event by setting the ComboList property to a string that describes the type of editing you want to use for that cell. The options are described below:
To edit the cell using a regular text box, set the ComboList property to an empty string. For example:
flex.ComboList = string.Empty
To edit the cell using a drop-down list, set the ComboList property to a string containing the available options, separated by pipe characters. For example:
flex.ComboList = "Item 1|Item 2|Item 3"
To edit the cell using a drop-down combo, set the ComboList property to a string containing the available options, separated by pipe characters ("|") and starting with a pipe character. For example:
flex.ComboList = "|Item 1|Item 2|Item 3"
To display an edit button, set the ComboList property to a string containing an ellipsis ("..."). Edit buttons look like regular push buttons, aligned to the right of the cell, with an ellipsis as a caption. When the user clicks on the edit button, the grid fires the CellButtonClick event. In this case, the user can't edit the cell contents directly. For example:
flex.ComboList = "..."
To display an edit button next to an editable cell, set the ComboList property to a string containing a pipe and an ellipsis ("|..."). In this case, you get a regular edit button but the user can also edit the cell contents directly. For example:
flex.ComboList = "|..."
Example:
The code below handles the BeforeEdit event and assigns a value to the ComboList property so that the grid displays buttons on every other row.
Private Sub _flex_BeforeEdit(sender As Object, e As RowColEventArgs)
_flex.ComboList = String.Empty
If e.Row Mod 2 = 0 Then
_flex.ComboList = "..."
End If
End Sub