blob: 364e067f3b92bb529a5439734dd4a0862c65a2ac (
plain)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
<?xml version="1.0" encoding="UTF-8"?>
<helpdocument version="1.0">
<!--
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
-->
<meta>
<topic id="text/sbasic/python/Python_Platform">
<title id="tit" xml-lang="en-US">Python : Platform class</title>
<filename>/text/sbasic/python/python_platform.xhp</filename>
</topic>
</meta>
<body>
<bookmark branch="index" id="N0508">
<bookmark_value>Platform;isLinux</bookmark_value>
<bookmark_value>Platform;isMacOsX</bookmark_value>
<bookmark_value>Platform;isWindows</bookmark_value>
<bookmark_value>Platform;ComputerName</bookmark_value>
<bookmark_value>Platform;OSName</bookmark_value>
<bookmark_value>API;ConfigurationAccess</bookmark_value>
<bookmark_value>Tools;GetRegistryContent</bookmark_value>
</bookmark>
<h1 id="N0509"><variable id="pythonplatform"><link href="text/sbasic/python/python_platform.xhp" name="platform01">Identifying the operating system</link></variable></h1>
<paragraph role="paragraph" id="N0510">Identifying the operating system can be performed with Python or Basic language.</paragraph>
<note id="N0511">ComputerName property is solely available for Windows. Basic calls to Python macros help overcome %PRODUCTNAME Basic limitations.</note>
<h2 id="N0512">Using a Python class:</h2>
<pycode>
<paragraph role="pycode" localize="false" id="N0513">import os, platform</paragraph>
<paragraph role="pycode" localize="false" id="N0514">class Platform():</paragraph>
<paragraph role="pycode" localize="false" id="N0515"> @property</paragraph>
<paragraph role="pycode" localize="false" id="N0516"> def ComputerName(self): return platform.node()</paragraph>
<paragraph role="pycode" localize="false" id="N0517"> @property</paragraph>
<paragraph role="pycode" localize="false" id="N0518"> def DirSeparator(self): return os.sep</paragraph>
<paragraph role="pycode" localize="false" id="N0519"> @property</paragraph>
<paragraph role="pycode" localize="false" id="N0520"> def isLinux(self): return (self.OSName=='Linux')</paragraph>
<paragraph role="pycode" localize="false" id="N0521"> @property</paragraph>
<paragraph role="pycode" localize="false" id="N0522"> def isMacOSX(self): return (self.OSName=='Darwin')</paragraph>
<paragraph role="pycode" localize="false" id="N0523"> @property</paragraph>
<paragraph role="pycode" localize="false" id="N0524"> def isWindows(self): return (self.OSName=='Windows')</paragraph>
<paragraph role="pycode" localize="false" id="N0525"> @property</paragraph>
<paragraph role="pycode" localize="false" id="N0526"> def OSName(self): return platform.system()</paragraph>
<paragraph role="pycode" localize="false" id="N0527"> @property</paragraph>
<paragraph role="pycode" localize="false" id="N0528"> def PathDelimiter(self): return os.pathsep</paragraph>
</pycode>
<h2 id="N0529">Using a Basic classmodule:</h2>
<tip id="NO529b">%PRODUCTNAME Basic lacks MacOS X native recognition. Platform identification is possible using %PRODUCTNAME Application Programming Interface (API).</tip>
<bascode>
<paragraph role="bascode" localize="false" id="N0530">Option Compatible</paragraph>
<paragraph role="bascode" localize="false" id="N0531">Option ClassModule</paragraph>
<paragraph role="bascode" localize="false" id="N0532">Option Explicit</paragraph>
<paragraph role="bascode" localize="false" id="N0533"></paragraph>
<paragraph role="bascode" localize="false" id="N0534">Public Property Get ComputerName As String</paragraph>
<paragraph role="bascode" localize="false" id="N0535"> If isWindows Then ComputerName = Environ("ComputerName")</paragraph>
<paragraph role="bascode" localize="false" id="N0536">End Property ' Platform.ComputerName</paragraph>
<paragraph role="bascode" localize="false" id="N0537"></paragraph>
<paragraph role="bascode" localize="false" id="N0538">Public Property Get DirSeparator As String</paragraph>
<paragraph role="bascode" localize="false" id="N0539"> DirSeparator = GetPathSeparator()</paragraph>
<paragraph role="bascode" localize="false" id="N0540">End Property ' Platform.DirSeparator</paragraph>
<paragraph role="bascode" localize="false" id="N0541"></paragraph>
<paragraph role="bascode" localize="false" id="N0542">Public Property Get IsLinux As Boolean</paragraph>
<paragraph role="bascode" localize="false" id="N0543"> isLinux = ( GetGUIType()=4 ) ' Applies to macOS as well </paragraph>
<paragraph role="bascode" localize="false" id="N0544">End Property ' Platform.isLinux</paragraph>
<paragraph role="bascode" localize="false" id="N0545"></paragraph>
<paragraph role="bascode" localize="false" id="M0542">Public Property Get IsMacOSX As Boolean</paragraph>
<paragraph role="bascode" localize="false" id="M0543"> isMacOSX = ( OSName="MAC" )</paragraph>
<paragraph role="bascode" localize="false" id="M0544">End Property ' Platform.isMacOSX</paragraph>
<paragraph role="bascode" localize="false" id="M0545"></paragraph>
<paragraph role="bascode" localize="false" id="N0546">Public Property Get IsWindows As Boolean</paragraph>
<paragraph role="bascode" localize="false" id="N0547"> isWindows = ( GetGUIType()=1 )</paragraph>
<paragraph role="bascode" localize="false" id="N0548">End Property ' Platform.isWindows</paragraph>
<paragraph role="bascode" localize="false" id="N0549"></paragraph>
<paragraph role="bascode" localize="false" id="N0550">Public Property Get OSName As String</paragraph>
<paragraph role="bascode" xml-lang="en-US" id="N0451"> ' Return platform name as "MAC", "UNIX", "WIN"</paragraph>
<paragraph role="bascode" xml-lang="en-US" id="N0551"> ' Inferred from "Tools.UCB.ShowHelperDialog" function</paragraph>
<paragraph role="bascode" localize="false" id="N0552"> With GlobalScope.Basiclibraries</paragraph>
<paragraph role="bascode" localize="false" id="M0552"> If Not .IsLibraryLoaded("Tools") Then .LoadLibrary("Tools")</paragraph>
<paragraph role="bascode" localize="false" id="O0552"> End With</paragraph>
<paragraph role="bascode" localize="false" id="N0553"> Dim keyNode As Object ' com.sun.star.configuration.ConfigurationAccess</paragraph>
<paragraph role="bascode" localize="false" id="M0553"> keyNode = Tools.Misc.GetRegistryKeyContent("org.openoffice.Office.Common/Help")</paragraph>
<paragraph role="bascode" localize="false" id="N0554"> OSName = keyNode.GetByName("System")</paragraph>
<paragraph role="bascode" localize="false" id="N0555">End Property ' Platform.OSName</paragraph>
<paragraph role="bascode" localize="false" id="N0556"></paragraph>
<paragraph role="bascode" localize="false" id="N0557">Public Property Get PathDelimiter As String</paragraph>
<paragraph role="bascode" localize="false" id="N0559"> Select Case OSName</paragraph>
<paragraph role="bascode" localize="false" id="N0560"> Case "MAC", "UNIX" : PathDelimiter = ":"</paragraph>
<paragraph role="bascode" localize="false" id="N0561"> Case "WIN" : PathDelimiter = ";"</paragraph>
<paragraph role="bascode" localize="false" id="N0562"> End Select</paragraph>
<paragraph role="bascode" localize="false" id="N0563">End Property ' Platform.PathDelimiter</paragraph>
</bascode>
<h2 id="N0564">Examples:</h2>
<paragraph role="paragraph" id="N0565">With Python</paragraph>
<paragraph role="paragraph" localize="false" id="N0566"><literal>>>> from <the_module> import Platform</literal></paragraph>
<paragraph role="paragraph" xml-lang="en-US" id="N0567"><literal>>>> print(Platform().isMacOSX) # object property</literal></paragraph>
<paragraph role="paragraph" localize="false" id="N0568"><literal>True</literal></paragraph>
<paragraph role="paragraph" xml-lang="en-US" id="N0569"><literal>>>> input(Platform().OSName) # object property</literal></paragraph>
<paragraph role="paragraph" localize="false" id="N0570"><literal>Darwin</literal></paragraph>
<paragraph role="paragraph" id="N0571">
From <menuitem>Tools – Macros - Run Macro...</menuitem> menu.</paragraph>
<pycode>
<paragraph role="pycode" localize="false" id="N0572">from <the_module> import Platform</paragraph>
<paragraph role="pycode" localize="false" id="N0573">import screen_io as ui</paragraph>
<paragraph role="pycode" localize="false" id="N0574">p = Platform()</paragraph>
<paragraph role="pycode" localize="false" id="N0575">ui.MsgBox(''.join(['isMacOS: ',str(p.isMacOSX)]),0,p.OSName)</paragraph>
</pycode>
<paragraph role="paragraph" id="N0576">With %PRODUCTNAME Basic</paragraph>
<bascode>
<paragraph role="bascode" localize="false" id="N0577">Sub Platform_example()</paragraph>
<paragraph role="bascode" xml-lang="en-US" id="N0578"> Dim p As New Platform ' instance of Platform class</paragraph>
<paragraph role="bascode" xml-lang="en-US" id="N0579"> MsgBox p.isLinux ' object property</paragraph>
<paragraph role="bascode" xml-lang="en-US" id="N0580"> Print p.isWindows, p.OSName ' object properties</paragraph>
<paragraph role="bascode" localize="false" id="N0581">End Sub ' Platform_example</paragraph>
</bascode>
<section id="relatedtopics">
<embed href="text/sbasic/python/python_screen.xhp#ioscreen"/>
<embed href="text/sbasic/shared/03132100.xhp#getguitype2"/>
<embed href="text/sbasic/shared/GetPathSeparator.xhp#getpathseparator01"/>
<!--WIP
<paragraph role="paragraph" id="N0585">
<link href="text/sbasic/python/python_from_basic.xhp">Basic to Python function calls</link>
</paragraph>-->
<embed href="text/sbasic/python/python_examples.xhp#pythonexamples2"/>
<embed href="text/sbasic/python/main0000.xhp#pythonscriptshelp"/>
</section>
</body>
</helpdocument>
|