-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFMPasteBoxTools.py
More file actions
532 lines (400 loc) · 13.6 KB
/
FMPasteBoxTools.py
File metadata and controls
532 lines (400 loc) · 13.6 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
# -*- coding: utf-8 -*-
from __future__ import print_function
"""Some tools which are needed by most files.
"""
import sys
import os
import re
import struct
# import traceback
import datetime
import unicodedata
import hashlib
#import xml.etree.ElementTree
#ElementTree = xml.etree.ElementTree
import pdb
import FMPasteBoxVersion
kwdbg = FMPasteBoxVersion.developmentversion
kwlog = FMPasteBoxVersion.developmentversion
import pprint
pp = pprint.pprint
import urllib
import objc
import Foundation
NSURL = Foundation.NSURL
NSFileManager = Foundation.NSFileManager
# NSUserDefaults = Foundation.NSUserDefaults
# NSString = Foundation.NSString
import AppKit
NSOpenPanel = AppKit.NSOpenPanel
NSAlert = AppKit.NSAlert
NSSavePanel = AppKit.NSSavePanel
NSFileHandlingPanelOKButton = AppKit.NSFileHandlingPanelOKButton
NSPasteboard = AppKit.NSPasteboard
# NSPasteboardCommunicationException = AppKit.NSPasteboardCommunicationException
# py3 stuff
py3 = False
try:
unicode('')
punicode = unicode
pstr = str
punichr = unichr
except NameError:
punicode = str
pstr = bytes
py3 = True
punichr = chr
def num2ostype( num ):
if num == 0:
return '????'
s = struct.pack(">I", num)
return makeunicode(s, "macroman")
def ostype2num( ostype ):
return struct.pack('BBBB', list(ostype))
def makeunicode(s, srcencoding="utf-8", normalizer="NFC"):
if type(s) not in (punicode, pstr):
s = str( s )
if type(s) != punicode:
s = punicode(s, srcencoding)
s = unicodedata.normalize(normalizer, s)
return s
def NSURL2str( nsurl ):
if isinstance(nsurl, NSURL):
return str(nsurl.absoluteString())
return nsurl
def getFileProperties( theFile ):
"""
"""
sfm = NSFileManager.defaultManager()
props = sfm.fileAttributesAtPath_traverseLink_( theFile, True )
if not props:
return {}
mtprops = props.mutableCopy()
mtprops.removeObjectsForKeys_( [
u"NSFileExtensionHidden",
u"NSFileGroupOwnerAccountID",
u"NSFileGroupOwnerAccountName",
u"NSFileOwnerAccountID",
u"NSFileOwnerAccountName",
#u"NSFilePosixPermissions",
#u"NSFileReferenceCount",
# u"NSFileSize",
#u"NSFileSystemFileNumber",
u"NSFileSystemNumber",
u"NSFileType",
# u"NSFileHFSCreatorCode",
# u"NSFileHFSTypeCode",
#u"NSFileCreationDate"
] )
return mtprops
def setFileProperties( theFile, props ):
sfm = NSFileManager.defaultManager()
return sfm.changeFileAttributes_atPath_( props, theFile )
def datestring_nsdate( dt=datetime.datetime.now() ):
now = str(dt)
now = now[:19]
now = now + " +0000"
return now
def setFileModificationDate( filepath, modfdt ):
l = getFileProperties( filepath )
date = Foundation.NSDate.dateWithString_( datestring_nsdate( modfdt ) )
l['NSFileModificationDate'] = date
setFileProperties( filepath, l)
folder, filename = os.path.split( filepath )
print( "Setting file(%s) modification date to %s" % (filename, repr(modfdt )))
def uniquepath(folder, filenamebase, ext, nfill=3, startindex=1, sep="_", always=True):
"""
"""
folder = os.path.abspath( folder )
if not always:
path = os.path.join(folder, filename + ext )
if not os.path.exists( path ):
return path
n = startindex
while True:
serialstring = str(n).rjust(nfill, "0")
filename = filenamebase + sep + serialstring + ext
fullpath = os.path.join(folder, filename)
if n >= 10**nfill:
nfill = nfill + 1
if not os.path.exists(fullpath):
return fullpath
n += 1
def gethashval( s ):
m = hashlib.sha1()
size = len(s)
t = b"blob %i\0%s" % (size, s)
m.update(t)
return (m.hexdigest(), size)
def cancelContinueAlert(title, message, butt1="OK", butt2=False):
"""Run a generic Alert with buttons "Weiter" & "Abbrechen".
Returns True if "Weiter"; False otherwise
"""
alert = NSAlert.alloc().init()
alert.setAlertStyle_( 0 )
alert.setInformativeText_( title )
alert.setMessageText_( message )
alert.setShowsHelp_( False )
alert.addButtonWithTitle_( butt1 )
if butt2:
# button 2 has keyboard equivalent "Escape"
button2 = alert.addButtonWithTitle_( butt2 )
button2.setKeyEquivalent_( unichr(27) )
f = alert.runModal()
return f == AppKit.NSAlertFirstButtonReturn
def errorDialog( message="Error", title="Some error occured..."):
return cancelContinueAlert(title, message)
def getFileDialog(multiple=False):
panel = NSOpenPanel.openPanel()
panel.setCanChooseFiles_(True)
panel.setCanChooseDirectories_(False)
panel.setAllowsMultipleSelection_(multiple)
rval = panel.runModalForTypes_( None )
if rval:
return [t for t in panel.filenames()]
return []
def getApplicationDialog():
panel = NSOpenPanel.openPanel()
panel.setCanChooseFiles_(True)
panel.setCanChooseDirectories_(False)
panel.setAllowsMultipleSelection_(False)
rval = panel.runModalForTypes_( ['app'] )
if rval:
l = [makeunicode(t.path()) for t in panel.URLs()]
return l[0]
return ""
def getFolderDialog(multiple=False):
panel = NSOpenPanel.openPanel()
panel.setCanChooseFiles_(False)
panel.setCanChooseDirectories_(True)
panel.setAllowsMultipleSelection_(multiple)
rval = panel.runModalForTypes_([])
if rval:
return [t for t in panel.filenames()]
return []
def saveAsDialog(path):
panel = NSSavePanel.savePanel()
if path:
panel.setDirectory_( path )
panel.setMessage_( u"Save as OPML" )
panel.setExtensionHidden_( False )
panel.setCanSelectHiddenExtension_(True)
panel.setRequiredFileType_( u"opml" )
if path:
if not os.path.isdir( path ):
folder, fle = os.path.split(path)
else:
folder = path
fle = "Untitled.opml"
rval = panel.runModalForDirectory_file_(folder, fle)
else:
rval = panel.runModal()
if rval == NSFileHandlingPanelOKButton:
return panel.filename()
return False
def datetimestamp( dt=None ):
# '2018-02-17 19:41:02'
if not dt:
dt = datetime.datetime.now()
now = str(dt)
now = now[:19]
d, t = now.split()
t = t.replace(':', '')
return (d,t)
def get_type_from_hexstring( hexstring ):
"""Extract the 4-char macroman type code from the pasteboard type name."""
h = int(hexstring, 16)
s = struct.pack(">I", h)
s = makeunicode(s, 'macroman')
return s
def get_hexstring_for_type( typ_ ):
"""
"""
s = struct.pack( "BBBB", typ_ )
i = struct.unpack( ">I", s)
return hex(i)
def get_type_from_intstring( intstring ):
h = int(intstring)
s = struct.pack(">I", h)
s = makeunicode(s, 'macroman')
return s
def get_flavor(s):
"""Return the 4-char type from a pasteboard name
"""
# seems like the standart naming scheme for the pasteboard server
re_pbtype = re.compile( u"CorePasteboardFlavorType 0x([A-F0-9]{,8})")
m = re_pbtype.match(s)
result = ""
if m:
t = m.groups()[0]
result = get_type_from_hexstring(t)
return result
def writePasteboardFlavour( folder, basename, ext, data ):
p = uniquepath(folder, basename, ext)
if data:
f = open ( p, 'wb')
f.write( data )
f.close()
# fmpa 18
# XMVL - 0x584D564C - Value Lists
# public.utf16-plain-text - Custom Menu Set Catalogue
# public.utf16-plain-text - Custom Menu Catalogue
# fmpa 15
# XML2 - 0x584D4C32 - generic xml for layout objects
# FMPA 11
# XMFN - 0x584D464E - Custom Functions
# FileMaker Advanced Pasteboard types
# XMFD - 0x584D4644 - fields
# XMTB - 0x584D5442 - basetables
# XMSC - 0x584D5343 - scripts
# XMSS - 0x584D5353 - script step
# XMLO - 0x584D4C4F - layout objects
# FMPA 18
# XMVL
# Custom menu set - XML text
# FileMaker Developer Pasteboard types
# beides binaerformate
# FTR5 - 0x46545235 -
# FMP5
class PasteboardType(object):
canonicalTypes = {
u'com.adobe.pdf': u'Apple PDF pasteboard type',
u'public.jpeg': u"CorePasteboardFlavorType 0x4A504547",
u'NeXT TIFF v4.0 pasteboard type': u'public.tiff',
# XML2
u'dyn.ah62d4rv4gk8zuxnqgk': u"CorePasteboardFlavorType 0x584D4C32",
}
def __init__(self, pbname, typ, dataType, name, fileExt):
self.pbname = pbname
self.typ = typ
self.dataType = dataType
self.name = name
self.fileExt = fileExt
self.canonicalType = self.canonicalTypes.get( pbname, pbname )
def __repr__(self):
return u"PasteboardType(%s, %s, %s, %s, %s, %s)" % (
repr(self.pbname),
repr(self.typ),
repr(self.dataType),
repr(self.name),
repr(self.fileExt),
repr(self.canonicalType),)
class PasteboardEntry(object):
def __init__(self, name, data, typ):
self.name = name
self.data = data
self.typ = typ
self.additionals = []
def __repr__(self):
return u"PasteboardEntry(%s, data[%i], %s, %s)" % (
repr(self.name),
len(self.data),
repr(self.typ),
repr(self.additionals))
fmpPasteboardTypes = {
u"CorePasteboardFlavorType 0x584D4C32":
PasteboardType(u"CorePasteboardFlavorType 0x584D4C32",
'XML2', 'fullXML', "Layout Objects", '.xml'),
u"CorePasteboardFlavorType 0x584D5442":
PasteboardType(u"CorePasteboardFlavorType 0x584D5442",
'XMTB', 'snippetXML', "Base Tables", '.xml'),
u"CorePasteboardFlavorType 0x584D4644":
PasteboardType(u"CorePasteboardFlavorType 0x584D4644",
'XMFD', 'snippetXML', "Fields", '.xml'),
u"CorePasteboardFlavorType 0x584D5343":
PasteboardType(u"CorePasteboardFlavorType 0x584D5343",
'XMSC', 'snippetXML', "Scripts", '.xml'),
u"CorePasteboardFlavorType 0x584D5353":
PasteboardType(u"CorePasteboardFlavorType 0x584D5353",
'XMSS', 'snippetXML', "Script Steps", '.xml'),
u"CorePasteboardFlavorType 0x584D464E":
PasteboardType(u"CorePasteboardFlavorType 0x584D464E",
'XMFN', 'snippetXML', "Custom Functions", '.xml'),
u"CorePasteboardFlavorType 0x584D4C4F":
PasteboardType(u"CorePasteboardFlavorType 0x584D4C4F",
'XMLO', 'snippetXML', "Layout Objects (obsolete)", '.xml'),
}
displaynameTypes = {}
# "Custom Functions" -> PasteboardType(u"CorePasteboardFlavorType 0x584D464E",...
for typeName in fmpPasteboardTypes:
typ = fmpPasteboardTypes[typeName]
displaynameTypes[typ.name] = typ
additionalFMPPasteboardTypes = {
u"CorePasteboardFlavorType 0x4A504547":
PasteboardType(u"CorePasteboardFlavorType 0x4A504547",
'JPEG', 'binaryData',
"Layout Objects JPEG Image", '.jpg'),
u'Apple PDF pasteboard type':
PasteboardType(u'Apple PDF pasteboard type',
'PDF', 'binaryData',
"Layout Objects PDF Image", '.pdf'),
u'com.adobe.pdf':
PasteboardType(u'com.adobe.pdf',
'PDF', 'binaryData',
"Layout Objects PDF Image", '.pdf'),
u'Apple PICT pasteboard type':
PasteboardType(u'Apple PICT pasteboard type',
'PICT', 'binaryData',
"Layout Objects PICT Image (obsolete)", '.pict'),
u'NeXT TIFF v4.0 pasteboard type':
PasteboardType(u'NeXT TIFF v4.0 pasteboard type',
'TIFF', 'binaryData',
"Layout Objects TIFF Image", '.tif'),
u'public.jpeg':
PasteboardType(u'public.jpeg',
'JPEG', 'binaryData',
"Layout Objects JPEG Image", '.jpg'),
u'public.tiff':
PasteboardType(u'public.tiff',
'TIFF', 'binaryData',
"Layout Objects TIFF Image", '.tif'),
}
def read_pb():
result = None
hashes = set()
additionals = []
pasteboard = NSPasteboard.generalPasteboard()
pbTypeNames = pasteboard.types()
# additionalFMPPasteboardTypes
for pbTypeName in pbTypeNames:
if kwlog:
print( "pbTypeName:", pbTypeName )
pbType = mainType = None
if pbTypeName in fmpPasteboardTypes:
pbType = fmpPasteboardTypes.get( pbTypeName )
mainType = True
elif pbTypeName in additionalFMPPasteboardTypes:
pbType = additionalFMPPasteboardTypes.get( pbTypeName )
mainType = False
if pbType == None:
continue
try:
# pdb.set_trace()
s = pasteboard.dataForType_( pbTypeName )
data = s.bytes().tobytes()
# dont load duplicate data
hashval, _ = gethashval( data )
if hashval in hashes:
continue
hashes.add( hashval )
if mainType:
data = makeunicode(data)
pbTypeName = pbType.canonicalType
pbEntry = PasteboardEntry(pbTypeName, data, pbType)
if mainType:
result = pbEntry
else:
additionals.append( pbEntry )
except Exception as v:
print( v )
# pdb.set_trace()
pp(locals())
print()
if result:
result.additionals = additionals
if kwlog:
print()
print( "result = " )
pp(result)
print()
return result