1
0
mirror of https://github.com/Toxblh/MTMR.git synced 2026-01-10 00:58:37 +00:00

Merge pull request #236 from markrickert/relative-file-paths

Relative file paths
This commit is contained in:
Anton Palgunov 2019-11-12 15:39:37 +00:00 committed by GitHub
commit 9e49cf8beb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 56 additions and 28 deletions

View File

@ -724,12 +724,20 @@ extension String {
}
var fileData: Data? {
return try? Data(contentsOf: URL(fileURLWithPath: self))
return try? Data(contentsOf: URL(fileURLWithPath: (self as NSString).expandingTildeInPath))
}
var fileString: String? {
var encoding: String.Encoding = .utf8
return try? String(contentsOfFile: self, usedEncoding: &encoding)
return try? String(contentsOf: URL(fileURLWithPath: (self as NSString).expandingTildeInPath), usedEncoding: &encoding)
}
var fileURL: URL {
return URL(fileURLWithPath: (self as NSString).expandingTildeInPath)
}
var appleScript: NSAppleScript? {
return NSAppleScript(source: self)
}
}
@ -749,16 +757,6 @@ enum Align: String, Decodable {
case right
}
extension String {
var fileURL: URL {
return URL(fileURLWithPath: self)
}
var appleScript: NSAppleScript? {
return NSAppleScript(source: self)
}
}
extension URL {
var appleScript: NSAppleScript? {
guard FileManager.default.fileExists(atPath: path) else { return nil }

View File

@ -26,6 +26,33 @@ class AppleScriptDefinitionTests: XCTestCase {
XCTAssertEqual(sourceStruct?.filePath, "/ololo/pew")
}
// This tests that users can pass paths to files with ~ in them
func testUserPath() {
let buttonNoActionFixture = """
[ { "type": "appleScriptTitledButton", "source": { "filePath": "~/pew" } } ]
""".data(using: .utf8)!
let result = try? JSONDecoder().decode([BarItemDefinition].self, from: buttonNoActionFixture)
guard case .appleScriptTitledButton(let source, _)? = result?.first?.type else {
XCTFail()
return
}
let sourceStruct = source as? Source
// gives you a string in the format of file:///Users/your_uer_name/pew
let expandedPath = URL(fileURLWithPath: NSString("~/pew").expandingTildeInPath) as URL
XCTAssertEqual(sourceStruct?.filePath?.fileURL, expandedPath)
}
// This tests that users can pass paths to images with ~ in them
func testUserImagePath() {
let relativeImagePath = """
[ { "filePath": "~/pew/image.png" } ]
""".data(using: .utf8)!
let result = try? JSONDecoder().decode([Source].self, from: relativeImagePath)
// gives you a string in the format of file:///Users/your_uer_name/pew/image.png
let expandedPath = URL(fileURLWithPath: NSString("~/pew/image.png").expandingTildeInPath) as URL
XCTAssertEqual(result?.first?.filePath?.fileURL, expandedPath)
}
func testRefreshInterval() {
let buttonNoActionFixture = """
[ { "type": "appleScriptTitledButton", "source": { "inline": "tell everything fine" }, "refreshInterval": 305} ]

View File

@ -128,7 +128,7 @@ The pre-installed configuration contains less or more than you'll probably want,
"type": "appleScriptTitledButton",
"refreshInterval": 60, //optional
"source": {
"filePath": "/Users/toxblh/Library/Application Support/MTMR/iTunes.nowPlaying.scpt",
"filePath": "~/Library/Application Support/MTMR/iTunes.nowPlaying.scpt",
// or
"inline": "tell application \"Finder\"\rif not (exists window 1) then\rmake new Finder window\rset target of front window to path to home folder as string\rend if\ractivate\rend tell",
// or
@ -156,6 +156,9 @@ Example of "CPU load" button which also changes color based on load value.
},
"align": "right",
"image": {
// Or you can specify a filePath here.
// Images will be resized to 24x24.
// "filePath": "~/myproject/myimage.jpg" // or "/fixed/path/to/the.png"
"base64":
"iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAA/1BMVEUAAADaACbYACfYACfjABzXACjYACfXACjYACfYACfYACfYACfdACLYACfXACjYACfVACv/AADXACjYACfYACfXACjYACfXACjaACXYACfYACfVACvYACfYACfZACbZACbYACfYACfZACb/AADYACfYACfVACrXACjVACu/AEDYACfYACfYACfXACjXACjYACfXACjYACfYACfYACfXACjYACfXACjYACfYACfZACbYACfYACfMADPYACfYACfYACfYACfYACfZACbXACjYACfYACfRAC7XACjYACfZACbWACnXACjXACjYACfTACzZACb/AADYACfYACfYACcAAAA+zneGAAAAU3RSTlMA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7afaa//ZPn3wD4/vje8PP/N7TebS0hgZhEAAAAJXRFWHRkYXRlOmNyZWF0ZQAyMDE4LTA4LTIyVDE3OjUyOjIyKzAyOjAwc2qUYAAAACV0RVh0ZGF0ZTptb2RpZnkAMjAxOC0wOC0yMlQxNzo1MjoyMiswMjowMAI3LNwAAAAZdEVYdFNvZnR3YXJlAHd3dy5pbmtzY2FwZS5vcmeb7jwaAAAAAElFTkSuQmCC"
},
@ -173,7 +176,7 @@ Example of "CPU load" button which also changes color based on load value.
"title": "stats",
"items": [
{ "type": "play" },
{ "type": "mute" },
{ "type": "mute" },
...
]
}

File diff suppressed because one or more lines are too long

View File

@ -3,7 +3,7 @@
{
"type": "exitTouchbar",
"image": {
"filePath": "/Users/toxblh/git/selfProjects/MTMR/Resources/logo.png"
"filePath": "~/git/selfProjects/MTMR/Resources/logo.png"
},
"width": 36, "align": "left" },
{ "type": "brightnessDown", "width": 36, "align": "left" },
@ -12,46 +12,46 @@
"type": "appleScriptTitledButton",
"source": {
"filePath":
"/Users/toxblh/git/selfProjects/MTMR/MTMR/AppleScripts/iTunes.nowPlaying.scpt"
"~/git/selfProjects/MTMR/MTMR/AppleScripts/iTunes.nowPlaying.scpt"
},
"action": "appleScript",
"actionAppleScript": {
"filePath": "/Users/toxblh/git/selfProjects/MTMR/MTMR/AppleScripts/iTunes.next.scpt"
"filePath": "~/git/selfProjects/MTMR/MTMR/AppleScripts/iTunes.next.scpt"
},
"refreshInterval": 1,
"image": {
"filePath": "/Users/toxblh/git/selfProjects/btt-touchbar-preset/icons/iTunes.png"
"filePath": "~/git/selfProjects/btt-touchbar-preset/icons/iTunes.png"
}
},
{
"type": "appleScriptTitledButton",
"source": {
"filePath":
"/Users/toxblh/git/selfProjects/MTMR/MTMR/AppleScripts/Spotify.nowPlaying.scpt"
"~/git/selfProjects/MTMR/MTMR/AppleScripts/Spotify.nowPlaying.scpt"
},
"action": "appleScript",
"actionAppleScript": {
"filePath":
"/Users/toxblh/git/selfProjects/MTMR/MTMR/AppleScripts/Spotify.next.scpt"
"~/git/selfProjects/MTMR/MTMR/AppleScripts/Spotify.next.scpt"
},
"refreshInterval": 1,
"image": {
"filePath": "/Users/toxblh/git/selfProjects/btt-touchbar-preset/icons/Spotify.png"
"filePath": "~/git/selfProjects/btt-touchbar-preset/icons/Spotify.png"
}
},
{
"type": "appleScriptTitledButton",
"source": {
"filePath":
"/Users/toxblh/git/selfProjects/MTMR/MTMR/AppleScripts/Vox.nowPlaying.scpt"
"~/git/selfProjects/MTMR/MTMR/AppleScripts/Vox.nowPlaying.scpt"
},
"action": "appleScript",
"actionAppleScript": {
"filePath": "/Users/toxblh/git/selfProjects/MTMR/MTMR/AppleScripts/Vox.next.scpt"
"filePath": "~/git/selfProjects/MTMR/MTMR/AppleScripts/Vox.next.scpt"
},
"refreshInterval": 1,
"image": {
"filePath": "/Users/toxblh/git/selfProjects/btt-touchbar-preset/icons/Vox.png"
"filePath": "~/git/selfProjects/btt-touchbar-preset/icons/Vox.png"
}
},
{