-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml2html.applescript
More file actions
82 lines (75 loc) · 3.84 KB
/
Copy pathxml2html.applescript
File metadata and controls
82 lines (75 loc) · 3.84 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
on open theFiles
my maybeAskSetDefault()
repeat with f in theFiles
my convertAndOpen(POSIX path of f)
end repeat
end open
on run
my maybeAskSetDefault()
try
set chosenFile to choose file with prompt "Выберите XML-файл счёта-фактуры / УПД для конвертации" of type {"public.xml", "xml"}
on error number -128
return
end try
my convertAndOpen(POSIX path of chosenFile)
end run
on maybeAskSetDefault()
set markerPath to (POSIX path of (path to home folder)) & "Library/Application Support/xml2html/.asked"
set alreadyAsked to false
try
do shell script "test -f " & quoted form of markerPath
set alreadyAsked to true
end try
if alreadyAsked then return
do shell script "mkdir -p " & quoted form of ((POSIX path of (path to home folder)) & "Library/Application Support/xml2html")
do shell script "touch " & quoted form of markerPath
set answer to button returned of (display dialog "Сделать «Viewer xml2html» программой по умолчанию для XML-файлов?" & return & return & "Двойной клик по XML будет сразу открывать конвертированный файл, без «Открыть с помощью». Также программа будет автоматически снимать блокировку системы безопасности с новых XML-файлов в папке Загрузки." buttons {"Не сейчас", "Да, сделать"} default button "Да, сделать")
if answer is "Да, сделать" then
set helperPath to POSIX path of ((path to resource "xml2htmlhelper") as text)
try
do shell script quoted form of helperPath & " both"
display dialog "Готово! «Viewer xml2html» назначен программой по умолчанию для XML-файлов." buttons {"OK"} default button 1
on error errMsg
display dialog "Не удалось завершить настройку:" & return & errMsg buttons {"OK"} default button 1 with icon stop
end try
end if
end maybeAskSetDefault
on findPython3()
set candidates to {"/opt/homebrew/bin/python3", "/usr/local/bin/python3", "/usr/bin/python3"}
repeat with p in candidates
try
do shell script "test -x " & quoted form of p & " && " & quoted form of p & " -c 1 2>&1"
return p
on error
-- пробуем следующий путь
end try
end repeat
return ""
end findPython3
on convertAndOpen(xmlPath)
set pyPath to my findPython3()
if pyPath is "" then
display dialog "На этом Mac не найден рабочий Python 3 — без него конвертация не работает." & return & return & "Чтобы исправить: откройте Terminal и выполните команду:" & return & "xcode-select --install" & return & return & "Дождитесь завершения установки (обычно 5–15 минут) и повторите открытие файла." buttons {"OK"} default button 1 with icon stop
return
end if
set scriptPath to POSIX path of ((path to resource "upd_to_html.py") as text)
set htmlPath to my replaceExtension(xmlPath, "html")
try
do shell script quoted form of pyPath & " " & quoted form of scriptPath & " " & quoted form of xmlPath & " " & quoted form of htmlPath
on error errMsg
display dialog "Не удалось сконвертировать файл:" & return & xmlPath & return & return & errMsg buttons {"OK"} default button 1 with icon stop
return
end try
do shell script "open " & quoted form of htmlPath
end convertAndOpen
on replaceExtension(p, newExt)
set AppleScript's text item delimiters to "."
set parts to text items of p
if (count of parts) > 1 then
set base to (items 1 thru -2 of parts) as string
else
set base to p
end if
set AppleScript's text item delimiters to ""
return base & "." & newExt
end replaceExtension