Copyq: Change case of selected text?

Created on 12 Mar 2017  路  17Comments  路  Source: hluk/CopyQ

Is it possible to change text that is selected in another program?

Thanks.

Most helpful comment

Solution that doesn't upper-case word like "a", "the", "for" etc. is (from this answer):

[Command]
Command="
    copyq:
    if (!copy())
      abort()

    // http://stackoverflow.com/a/6475125/454171
    String.prototype.toTitleCase = function() {
      var i, j, str, lowers, uppers;
      str = this.replace(/([^\\W_]+[^\\s-]*) */g, function(txt) {
        return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
      });

      // Certain minor words should be left lowercase unless 
      // they are the first or last words in the string
      lowers = ['A', 'An', 'The', 'And', 'But', 'Or', 'For', 'Nor', 'As', 'At', 
      'By', 'For', 'From', 'In', 'Into', 'Near', 'Of', 'On', 'Onto', 'To', 'With'];
      for (i = 0, j = lowers.length; i < j; i++)
        str = str.replace(new RegExp('\\\\s' + lowers[i] + '\\\\s', 'g'), 
          function(txt) {
            return txt.toLowerCase();
          });

      // Certain words such as initialisms or acronyms should be left uppercase
      uppers = ['Id', 'Tv'];
      for (i = 0, j = uppers.length; i < j; i++)
        str = str.replace(new RegExp('\\\\b' + uppers[i] + '\\\\b', 'g'), 
          uppers[i].toUpperCase());

      return str;
    }

    var text = str(clipboard())

    var newText = text.toTitleCase();
    if (text == newText)
      abort();

    copy(newText)
    paste()"
GlobalShortcut=meta+alt+t
Icon=\xf034
Name=To Title Case

All 17 comments

Following command could help (copy/paste the code to command list in Command dialog F6 and change the shortcut).

[Command]
Command="
    copyq:
    if (!copy())
      abort()

    var text = str(clipboard())

    var newText = text.toUpperCase()
    if (text == newText)
      newText = text.toLowerCase()

    if (text == newText)
      abort();

    copy(newText)
    paste()"
GlobalShortcut=meta+ctrl+u
Icon=\xf034
Name=Toggle Upper/Lower Case

Very nice. Thanks very much! Would there be a Title Case option as well?

Solution that doesn't upper-case word like "a", "the", "for" etc. is (from this answer):

[Command]
Command="
    copyq:
    if (!copy())
      abort()

    // http://stackoverflow.com/a/6475125/454171
    String.prototype.toTitleCase = function() {
      var i, j, str, lowers, uppers;
      str = this.replace(/([^\\W_]+[^\\s-]*) */g, function(txt) {
        return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
      });

      // Certain minor words should be left lowercase unless 
      // they are the first or last words in the string
      lowers = ['A', 'An', 'The', 'And', 'But', 'Or', 'For', 'Nor', 'As', 'At', 
      'By', 'For', 'From', 'In', 'Into', 'Near', 'Of', 'On', 'Onto', 'To', 'With'];
      for (i = 0, j = lowers.length; i < j; i++)
        str = str.replace(new RegExp('\\\\s' + lowers[i] + '\\\\s', 'g'), 
          function(txt) {
            return txt.toLowerCase();
          });

      // Certain words such as initialisms or acronyms should be left uppercase
      uppers = ['Id', 'Tv'];
      for (i = 0, j = uppers.length; i < j; i++)
        str = str.replace(new RegExp('\\\\b' + uppers[i] + '\\\\b', 'g'), 
          uppers[i].toUpperCase());

      return str;
    }

    var text = str(clipboard())

    var newText = text.toTitleCase();
    if (text == newText)
      abort();

    copy(newText)
    paste()"
GlobalShortcut=meta+alt+t
Icon=\xf034
Name=To Title Case

Thanks, hluk. I really appreciate your help.

2 observances: first, this last code, while useful, seems to be more Sentence case than Title case. It uppercases only the first word of a selection?

Second, neither of these work from the menu. They must be invoked via hotkey. I'm sure that's a focus issue.

Thanks again.

For me the second command upper-cases all words except the few.

E.g. this:

Do androids dream of electric sheep?

changes to:

Do Androids Dream of Electric Sheep?

Focusing windows from tray menu really didn't work well -- I fixed the bug recently. If you're on Windows you can try this version.

I'm on Linux. I tried Title case again, in a different program, and I'm still getting only the first word capitalized.

What's the app you try this in? I tested this in Gedit, Chromium and editor in CopyQ.

The updated packages for Linux are here (version should be 2.9.0.r224.g46623dc3).

I'm in 2.7.1, and I tried into Geany and Leafpad. Let me update CopyQ and I'll get back here.

Thanks.

BTW, If you're on Ubuntu you can now use new official PPA for CopyQ (though it doesn't yet have the fix for pasting from tray menu).

Sorry, I have no idea what to do with those files. The latest CopyQ in the repository is the one I have.

On Ubuntu run following commands in terminal to add package archive update system packages and install CopyQ. It should update to version 2.9.0.

sudo add-apt-repository ppa:hluk/copyq
sudo apt update
sudo apt install copyq

Ok, thanks. That got me 2.9.0. Still only capitalizing the first word of the selection, in Leafpad, using a global keyboard hotkey.

I just tried this with CopyQ 2.9.0 in Leafpad and seems to work for me. I select a sentence in Leafpad, press Win+Alt+T (this triggers the "To Title Case" command) and the firsts letter of each word in the selection changes case.

Are you sure the right command is triggered and the shortcut is correctly set? Do you have any other commands in Command dialog?

Perfect. I began to suspect I hadn't installed the command correctly. I did it manually, and messed it up. Then I saw the Import function and redid it, and now it's working great. :)

Thanks much for seeing this through! The actions on existing text can be a big help a lot of times. I used to use them a long time ago with CLCL, now I can again.

BW,
Paul

Glad it works for you. You might find some useful commands on this wiki page. I update the page whenever I find something useful.

Focusing windows from tray menu really didn't work well -- I fixed the bug recently. If you're on Windows you can try this version.

_Artifacts older than 6 months are automatically deleted._
Can you share the version you mentioned fixes the bug in Windows?
Thanks a lot for these useful commands for CopyQ. Greetings

Focusing windows from tray menu really didn't work well -- I fixed the bug recently. If you're on Windows you can try this version.

_Artifacts older than 6 months are automatically deleted._
Can you share the version you mentioned fixes the bug in Windows?
Thanks a lot for these useful commands for CopyQ. Greetings

This is very old issue and is fixed in released versions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bsucker98 picture bsucker98  路  4Comments

pacid picture pacid  路  3Comments

happysurf picture happysurf  路  9Comments

gatopeich picture gatopeich  路  3Comments

davidawad picture davidawad  路  9Comments