/
MuseScore.xba
110 lines (77 loc) · 2.96 KB
/
MuseScore.xba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
<script:module xmlns:script="http://openoffice.org/2000/script" script:name="MuseScore" script:language="StarBasic">REM ***** BASIC *****
option Explicit
Sub MuseScore
dim o as Object
dim msczUrl as String, graphicUrl as String
on error goto labelErrorHandler
' initialization
initExample
' get current selection
o = thisComponent.getCurrentSelection
if o.supportsService("com.sun.star.text.TextRanges") then
' cursor/selection is in text
msczUrl = userSelectExampleName(true,"","*.mscz")
if msczUrl = "" then
exit Sub
end if
graphicUrl = changeExtension(msczUrl,"png")
' (re)generate the graphic if necessary
msczToGraphic(ConvertFromURL(msczUrl),ConvertFromURL(graphicUrl),false)
' insert the example
insertExample(graphicUrl,msczUrl,"")
elseif o.supportsService("com.sun.star.text.TextGraphicObject") then
' cursor/selection is a graphic
updateMuseScore(o)
end if
exit Sub
labelErrorHandler:
myMsgBox("An unknown error occurred")
End Sub
' regenerate graphic if necessary
Sub updateMuseScore (o as Object)
dim graphicUrl as String
graphicUrl = msczToGraphic(ConvertFromURL(o.HyperLinkUrl),ConvertFromURL(o.GraphicURL),false)
if graphicUrl <> "" then
updateExample(o,graphicUrl)
end if
End Sub
' converts MSCZ to PNG
' returns URL of PNG
' returns "" on failure or if PNG was already up to date
Function msczToGraphic (msczPath as String, graphicPath as String, force as Boolean) as String
dim args as String, pngPath as String
msczToGraphic = ""
if not newer(graphicPath,msczPath) or force then
' convert MSCZ to PNG
args = "-T 0" + " -r " + pngResolution + " " + quote+msczPath+quote + " -o " + quote+graphicPath+quote
if myShell("mscore",6,args,true) <> 0 then
myMsgBox("unable to convert MSCZ to PNG")
exit Function
end if
pngPath = basePath(graphicPath) + "-1.png"
if not newer(pngPath,msczPath) then
myMsgBox("unable to convert MSCZ to PNG")
exit Function
end if
' MuseScore creates graphic with page 1 only; rename to main filename
if FileExists(graphicPath) then
on error goto labelFileBusy
SFA.kill(graphicPath)
on error goto 0
end if
SFA.move(pngPath,graphicPath)
' trim PNG
'args = "-trim -units PixelsPerInch -density 300 " + quote+graphicPath+quote
'if myShell("mogrify",6,args,true) <> 0 then
' myMsgBox("unable to trim PNG")
' exit Function
'end if
msczToGraphic = ConvertToURL(graphicPath)
end if
exit Function
labelFileBusy:
myMsgBox("Unable to overwrite "+graphicPath)
End Function
</script:module>