Repository navigation
Expand file tree
/
Copy pathAutoSvg2Png.ps1
More file actions
164 lines (142 loc) · 3.96 KB
/
Copy pathAutoSvg2Png.ps1
File metadata and controls
164 lines (142 loc) · 3.96 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
# Define the mutex name
$mutexName = "Global\AutoSvg2Png.Mutex"
# Create or open the mutex
$global:mutex = New-Object -TypeName System.Threading.Mutex($false, $mutexName)
$global:lastPath = ''
$global:lastTime = Get-Date
# Inkscape
function GetInkscape()
{
$cmd = 'inkscape.exe'
$exist = Get-Command $cmd -ErrorAction SilentlyContinue
if (-not $exist)
{
$cmd = "$env:ProgramFiles\inkscape\bin\inkscape.exe"
$exist = Test-Path -Path $cmd
}
if (-not $exist)
{
$cmd = "$env:ProgramFiles(x86)\inkscape\bin\inkscape.exe"
$exist = Test-Path -Path $cmd
}
if (-not $exist)
{
$cmd = $null
Write-Host "Error: inkscape not found"
}
return $cmd
}
function DoConvert()
{
param
(
[string]$Cmd,
[string]$Path
)
try
{
# Wait to acquire the mutex
$acquired = $global:mutex.WaitOne()
if ($acquired)
{
$samePath = $global:lastPath -eq $Path
$currTime = Get-Date
if ($samePath)
{
$timeDiff = New-TimeSpan -Start $global:lastTime -End $currTime
$seconds = $timeDiff.TotalSeconds
$sameTime = $seconds -le 5
if ($sameTime)
{
return
}
}
# Run the processing script and pass the new file as an argument
$outputPath = [System.IO.Path]::ChangeExtension($Path, 'png')
& $Cmd --export-type=png --export-dpi=300 --export-filename=$outputPath $Path
$trials = 1
while (-not (Test-Path -Path $outputPath))
{
Sleep -Milliseconds 100
}
Set-Clipboard -Path $outputPath
$time = [string]::Format("{0:hh:mm:ss}", $(Get-Date))
Write-Host "[$time] File copied: $outputPath"
$global:lastPath = $Path
$global:lastTime = Get-Date
}
}
finally
{
if ($acquired)
{
# Release the mutex
$global:mutex.ReleaseMutex()
}
}
}
function DoWatch()
{
param
(
[string]$Cmd,
[string]$Folder
)
# Define the folder to watch
$folderToWatch = $Folder
# Create a FileSystemWatcher object
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = $folderToWatch
$watcher.Filter = "*.svg" # Monitor svg file types
$watcher.EnableRaisingEvents = $true
# Monitor for creation and changes
$watcher.NotifyFilter = [System.IO.NotifyFilters]'FileName, LastWrite'
# Define the action to take when a file is created
$onCreatedAction = {
param ($source, $eventArgs)
# Get the full path of the new file
$path = $eventArgs.FullPath
# Log the file creation detection (optional)
Write-Output "File created: $path"
DoConvert -Cmd $cmd -Path $path
}
# Define the action to take when a file is changed
$onChangedAction = {
param ($source, $eventArgs)
# Get the full path of the changed file
$path = $eventArgs.FullPath
# Log the file change detection (optional)
Write-Output "File changed: $path"
DoConvert -Cmd $cmd -Path $path
}
try
{
# Register the event handler for the 'Created' event
Register-ObjectEvent $watcher Created -Action $onCreatedAction
# Register the event handler for the 'Changed' event
Register-ObjectEvent $watcher Changed -Action $onChangedAction
# Keep the script running to monitor the folder
Write-Output "Monitoring folder: $folderToWatch"
while ($true)
{
Start-Sleep 1
}
}
finally
{
Get-EventSubscriber | Unregister-Event
}
}
try
{
$cmd = GetInkscape
if ($cmd)
{
DoWatch -Cmd $cmd -Folder 'Z:\'
}
}
finally
{
# Dispose of the mutex object
$global:mutex.Dispose()
}