[Solved] How to store the result of copy in a variable

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Alkemiisto
Posts: 4
Joined: Sat Jul 03, 2021 3:33 pm
Location: Brazil

[Solved] How to store the result of copy in a variable

Post by Alkemiisto »

Hi.

I'm new here.
And I'm just a beginner in OpenOffice Macros.

I'm trying to copy some words from a text and attribute them to some variables.


I'm using:
dim args1(1) as new com.sun.star.beans.PropertyValue
args1(0).Name = "Count"
args1(0).Value = 1 ' or other number of letters
args1(1).Name = "Select"
args1(1).Value = true
dispatcher.executeDispatch(document, ".uno:GoRight", "", 0, args1())
rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:Copy", "", 0, Array())

CopyResult$ = ' where to I get the result of Copy?

So, how can I attribute the result of "copy" to a variable?

Thanks in advance.
Last edited by MrProgrammer on Tue Jul 13, 2021 3:57 pm, edited 1 time in total.
Reason: Tagged ✓ [Solved]
OpenOffice 4.1.10 on Windows 10
FJCC
Moderator
Posts: 9274
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: How to attribute the result of copy to a variable

Post by FJCC »

You are trying to use the dispatch calls from recorded macros to write your own macro and that is probably not a good idea. User-written macros should use the Application Programming Interface (API). A good source for information about that is here.
Can you explain what you are trying to accomplish? It looks like you want to do something with the currently selected text.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
User avatar
MrProgrammer
Moderator
Posts: 4905
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: How to attribute the result of copy to a variable

Post by MrProgrammer »

Alkemiisto wrote:
dim args1(1) as new com.sun.star.beans.PropertyValue
args1(0).Name = "Count"  : args1(0).Value = 1 
args1(1).Name = "Select" : args1(1).Value = true
dispatcher.executeDispatch(document, ".uno:GoRight", "", 0, args1())
dispatcher.executeDispatch(document, ".uno:Copy"   , "", 0, Array())
This macro was presumably created by Tools → Macros → Record. This feature records actions taken in the standard user interface. But the user interface — the menus you use, the cells you select, the actions you take — has no concept of "variables". Hence no recorded macro ever involves variables. Recorded macros cannot perform tests, thus constructs such as IF … THEN … ELSE or DO WHILE … are not available. These are significant restrictions, nontheless it is possible to record many many useful macros.

The other macro interface, Tools → Macros → Organize macros → «Language» allows you to write macros in a programming language and use the UNO Application Programming Interface. All of the languages provide variables, and you can use them in a written macro. However writing macros is harder than recording macros by a factor of about 100, I'd say. The book which FJCC has linked will get you started if you want to use that approach.

You should explain your goal. Perhaps a recorded macro can achieve it.
XY Problem
Alkemiisto wrote:So, how can I attribute the result of "copy" to a variable?
I do very little macro writing and have never used this interface myself, but I think XTransferable is what you would use. This seems to me as if it's an advanced topic in macro writing.
Mr. Programmer
AOO 4.1.7 Build 9800, MacOS 13.6.3, iMac Intel.   The locale for any menus or Calc formulas in my posts is English (USA).
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: How to attribute the result of copy to a variable

Post by JeJe »

copy puts something on the system clipboard and is the same as copy and paste in any other program.

For what you want to do start with these:

https://wiki.openoffice.org/wiki/Writer/API/View_cursor

https://wiki.openoffice.org/wiki/Writer/API/Text_cursor
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: How to attribute the result of copy to a variable

Post by JeJe »

Code: Select all

vc = thiscomponent.currentcontroller.viewcursor 'assign a variable to the view cursor
vc.goright(1,true) 'using this go right 1 character and keep the selection (the 'true')
str = vc.string 'assign the selected text to a variable called str
msgbox str 'show the variable in a message box
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: How to attribute the result of copy to a variable

Post by JeJe »

using xtransferable to assign a variable with the text formatting as well as the text string:

Code: Select all

'each document window/frame has its own "controller" which you use to access things like the window's view cursor
ccontroller = thiscomponent.currentcontroller 'assign a variable to the current/active controller
vc = ccontroller.viewcursor 'assign a variable to the controller's view cursor
vc.goright(1,true) 'using this go right 1 character and keep the selection (true)
trans = ccontroller.gettransferable 'assign a variable to the text and all its formatting
vc.goright 10,false 'move the view cursor to a different place
ccontroller.inserttransferable(trans) 'insert the assigned text and formatting
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Alkemiisto
Posts: 4
Joined: Sat Jul 03, 2021 3:33 pm
Location: Brazil

Re: How to attribute the result of copy to a variable

Post by Alkemiisto »

Thanks you so much.
Yes. I tryed the Macro recording (Tools → Macros → Record). I see now that it was not a good idea.
I'll explain my goal:
I like to play the guitar and to sing along.
I have a lot of song lyrics with chord symbols and I want to put them all in a more confortable tonality for me.
As they are hundreds of lyrics, I think it will better and more quick if I use a Macro for that.
I did one once for the old windows XP, but it does not work any more.
First of all it asked for the original tonality and for the destiny tonality.
Then it readed all the odd lines of the lyric and looked for chord symbols. And changed the chords to the new tonality.
Now I tried to make it for OpenOffice Writer, but I tryed to used recorded Macro and it did not work properly.
Does similar one already exist here in your macros library?
Meanwhile I'll read about API in wikipedia.
Thanks again.
OpenOffice 4.1.10 on Windows 10
User avatar
MrProgrammer
Moderator
Posts: 4905
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: How to attribute the result of copy to a variable

Post by MrProgrammer »

Alkemiisto wrote:I like to play the guitar and to sing along. I have a lot of song lyrics with chord symbols and I want to put them all in a more confortable tonality for me.
Many websites have been created for this purpose. Some examples (links may break):
http://tabtransposer.com/
http://chords.kytara.cz/transpose-music/
http://www.statistics101.net/chordsmith/
http://www.chordchanger.com/
You can no doubt find dozens of others. Using existing software will be much easier than writing macros.

If this solved your problem please go to your first post use the Edit button and add [Solved] to the start of the subject field. Select the green checkmark icon at the same time.
Mr. Programmer
AOO 4.1.7 Build 9800, MacOS 13.6.3, iMac Intel.   The locale for any menus or Calc formulas in my posts is English (USA).
John_Ha
Volunteer
Posts: 9584
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK

Re: How to attribute the result of copy to a variable

Post by John_Ha »

With the greatest respect to Mr Programmer's far greater knowledge than mine, I would suggest that no-one actually writes macros - they find something close and hack it. Well, that's what I do!

Andrew Pitonyak's free download book Open Office Macros has hundreds of examples and a wealth of copyable and eminently hackable macros.
LO 6.4.4.2, Windows 10 Home 64 bit

See the Writer Guide, the Writer FAQ, the Writer Tutorials and Writer for students.

Remember: Always save your Writer files as .odt files. - see here for the many reasons why.
Alkemiisto
Posts: 4
Joined: Sat Jul 03, 2021 3:33 pm
Location: Brazil

Re: How to attribute the result of copy to a variable

Post by Alkemiisto »

"MrProgrammer wrote:
"Many websites have been created for this purpose. Some examples (links may break):"


Good sugestion.
But I tested two of them and they got errors. Maybe they work well about english lyrics.
Not about portuguese lyrics (my first language).
When the program finds a "A" or a "E" separated by spaces in the text of the lyrics, it handles it as a chord.
But I think I'll use one of them for now and make the necessary corrections. I think it will be worthwhile.
Thanks.

" John_Ha wrote:
"they find something close..."

Do you know a close macro that could help me?
Thanks.
OpenOffice 4.1.10 on Windows 10
User avatar
MrProgrammer
Moderator
Posts: 4905
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: How to attribute the result of copy to a variable

Post by MrProgrammer »

Alkemiisto wrote:When the program finds a "A" or a "E" separated by spaces in the text of the lyrics, it handles it as a chord.
Perhaps it suffices to write "A" and "E" as ".A" and ".E" or "_A" and "_E". English will presumably have the same difficulty with the article "a".
Alkemiisto wrote:Not about portuguese lyrics (my first language).
My web search would have favored sites in English, so you may find better results for Portuguese.

If none prove suitable, and you want to spend a day or so on the project you could create the lyrics in Writer, inserting Mail Merge fields for the chords. If the first chord is the tonic, and you use the tonic 25 other times in the song, you would use the same field for each one. Similarly, you would use the same field for all the dominant chords. The database you use for the merge will have twelve fields for the twelve notes. I would use field names 0 through 11 for the notes, but you might prefer Solfège. For the key of C, the the tonic mail merge field (0 or Do) will be C and the dominant field (7 or Sol) will be G. To use a different key, create a second record in the database with different notes for the tonic, dominant, etc. The record for G will have G as the tonic field and D as the dominant field. You will want as many records as the keys that you use, perhaps half a dozen but potentially all twelve. You select the record for the merge when you perform it with File → Print → Mail Merge? → Yes. Read about Mail Merge in Help → Index or in User Guides (PDF) or searching for topics about it in the Writer Forum. Questions about Mail Merge should go in a new topic in that forum. I would think it will be more enjoyable to spend the time playing music instead of fighting with Writer, a database, and the Mail Merge feature.
Alkemiisto wrote:But I think I'll use one of them for now and make the necessary corrections.
If this solved your problem please go to your first post use the Edit button and add [Solved] to the start of the title. You can select the green checkmark icon at the same time.
John_Ha wrote:I would suggest that no-one actually writes macros …
I don't do much with macros, but did develop the "Favorites" extension a year ago. I wrote all of the code, though I did look at examples of how to use the API for dialogs and controls. However, I've been a programmer for over 50 years, thus writing code is quite natural.
Mr. Programmer
AOO 4.1.7 Build 9800, MacOS 13.6.3, iMac Intel.   The locale for any menus or Calc formulas in my posts is English (USA).
Alkemiisto
Posts: 4
Joined: Sat Jul 03, 2021 3:33 pm
Location: Brazil

Re: How to attribute the result of copy to a variable

Post by Alkemiisto »

MrProgrammer wrote:
...you could create the lyrics in Writer, inserting Mail Merge fields for the chords.
Thanks. It's a good suggestion. But all the lyrics (very old ones, written one by one when they were not so easy to find) are in a writer (.odt) text and they have also the chord symbols.
To change them all the chord symbols to fields would be very hard to do. That's why I'd like to make a new version of the old Macro I did for Microsoft World (XP) in the past, since all the lyricas lines are in the even and the chord symbols are in the odd ones.
" MrProgrammer wrote:
I would think it will be more enjoyable to spend the time playing music instead of fighting with Writer, a database, and the Mail Merge feature.
I respect your point of view, and you may laugh but I think to learn with Writer, creating a beautiful and usefull Macro, to me is so enjoyable as to play and sing along a song. :D

;) Ok! Just kidding... :lol: :lol: :lol: Play and sing are so much more enjoyable! :)
" MrProgrammer wrote:
If this solved your problem please go to your first post use the Edit button and add [Solved] to the start of the title. You can select the green checkmark icon at the same time.
Ok. I'll close the topic after a while.

Thank you all very much for your kind attention.
If you allow me, new doubts, I'm back.
OpenOffice 4.1.10 on Windows 10
Post Reply