5

I've made many AppleScript module for TextWrangler but this time I face a "stupid" problem: The idea: if nothing is selected in the text, set the selection to all the text. I've made many tests, without any succes. Here is the last one:

tell application "TextWrangler"
    set my_selection to (get selection)
    set nb_mot to count words of (my_selection)

    if nb_mot < 1 then
        tell application "System Events"
            keystroke "a" using command down
            display dialog "Select all"
            delay 1
        end tell
        set my_selection to (get selection)

        delay 2
        set nb_new_mot to count words of (my_selection)
        display dialog "After select all " & nb_new_mot
    end if

    set var_1 to (replace "(" using "(" searching in my_selection options {search mode:literal, starting at top:false, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false})
end tell

When I select a part of the text, it's okay. When nothing is selected, I enter the if block, but this give me an empty selection.

Any idea how I can grab only the selected text when there is something and perform a select all and copy that if no text is selected?

2

The reason you get an empty selection, i.e. when nothing is selected and set my_selection to (get selection) returns, e.g., insertion point before character 1 of text document 1, the if statement block fails with the

tell application "System Events"
    keystroke "a" using command down

portion of the code because TextWrangler doesn't have focus.

The keystroke command goes to whatever has focus, so before you have System Events keystroke something, activate the target first, e.g.:

tell application "TextWrangler"
    activate
    -- delay 1 -- # Uncomment and or adjust the value of the 'delay' command as/if necessary.
    set my_selection to (get selection)
    set nb_mot to count words of (my_selection)

    if nb_mot < 1 then
        tell application "System Events"
            keystroke "a" using command down
            display dialog "Select all"
...

That said, you can omit the activate command and use the following example AppleScript code:

tell application "TextWrangler"
    set my_selection to selection
    set nb_mot to count words of my_selection

    if nb_mot < 1 then
        set my_selection to characters 1 thru -1 of text document 1
        set nb_new_mot to count words of my_selection
    end if

    set var_1 to (replace "(" using "(" searching in my_selection options {search mode:literal, starting at top:false, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false})
end tell

Note for clarity, I've remove the display dialog and delay commands along with all of System Events code, and other unnecessary code, as the following line of code is all that's necessary if the nb_mot is < 1:

set my_selection to characters 1 thru -1 of text document 1

The Event Log and Result of this example AppleScript code is:

tell application "TextWrangler"
    get selection
        --> insertion point before character 1 of text document 1
    count every word of insertion point before character 1 of text document 1
        --> 0
    get characters 1 thru -1 of text document 1
        --> characters 1 thru 499 of text document 1
    count every word of characters 1 thru 499 of text document 1
        --> 70
    replace "(" using "(" searching in characters 1 thru 499 of text document 1 options {search mode:literal, starting at top:false, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false}
        --> 3
end tell
Result:
3

As you can see it replaces the three ( in:

set var_1 to (replace "(" using "(" searching in my_selection options {search mode:literal, starting at top:false, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false})
  • Unfortunatly, this change nothing :( – Peter Dec 17 '17 at 23:46
  • Another point: if I perform a keystroke "a" using command down then a keystroke "c" using command down, this will cut the text to the clipboard. But here, he clipboard stay empty. Meaning the cmd+A and Cmd+C has no effect (and so I can't get the new selection). Strange... – Peter Dec 17 '17 at 23:50
  • @Peter, I beg to differ with you, it does change things, however I've updated my answer because there is absolutely no need whatsoever to use UI Scripting when set my_selection to characters 1 thru -1 of text document 1 is all that necessary when if nb_mot < 1! – user3439894 Dec 18 '17 at 0:04
  • With the new answer, it works. Only with the activate, it does nothing more, but wih "set my_selection to characters 1 thru -1 of text document 1" that's OK. Thanks! – Peter Dec 18 '17 at 1:28
  • 1
    @Peter, The ⌘A ⌘C works for me because I'm using a supported version of TextWrangler on a supported version of OS X. From TextWrangler, TextWrangler is not compatible with macOS 10.13 (High Sierra), and is in all likelihood why it's not working for you. You might want to switch to BBEdit. In any case, my comment regarding UI Scripting stands and I'm glad I could offer you one of other ways possible to get things working for you. – user3439894 Dec 18 '17 at 12:26
2

This works for me using the latest version of Sierra

property currentSelection : ""
property selectAll : ""
property oldClipboard : ""

copy (the clipboard) to oldClipboard

tell application "TextWrangler"
    set currentSelection to contents of selection
    if currentSelection is not "" then
        set the clipboard to currentSelection
    else
        tell its document 1 -- assuming the document in question is document 1
            select every text
        end tell
        set selectAll to contents of selection
        set the clipboard to selectAll
    end if
end tell
  • Problem is that you loose the previous content of the clipboard while with user3439894's answer, you preserve the clipboard content. – Peter Dec 18 '17 at 1:29
  • I just updated the code to copy the old clipboard content To be stored in a property variable which you can access if needed – wch1zpink Dec 18 '17 at 1:41
  • @Peter You can also comment out the copy commands If you don't need them – wch1zpink Dec 18 '17 at 1:59
  • your code is OK why a small change: in the else, rather than tell its document1... and so on, just put 2 lines: "select every text of text document 1" and "set my_selection to (get selection)". Strangly, under OSX 10.13.2, the "select every text" after the "tell" give a message telling text is unknown. But maybe that's a TextWrangler issue as it not very "stable" on High Sierra. Thanks! – Peter Dec 18 '17 at 10:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .