-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8a3d18f
commit 26305ac
Showing
13 changed files
with
876 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// swift-tools-version:5.1 | ||
// The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "clipboard", | ||
dependencies: [ | ||
// Dependencies declare other packages that this package depends on. | ||
// .package(url: /* package url */, from: "1.0.0"), | ||
], | ||
targets: [ | ||
// Targets are the basic building blocks of a package. A target can define a module or a test suite. | ||
// Targets can depend on other targets in this package, and on products in packages which this package depends on. | ||
.target( | ||
name: "clipboard", | ||
dependencies: []), | ||
.testTarget( | ||
name: "clipboardTests", | ||
dependencies: ["clipboard"]), | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# clipboard | ||
A simple command line tool to Paste PNG into files, much like pbpaste does for text. | ||
|
||
## Save clipboard to local | ||
If clipboard content is PNG, save it userhome directory named "image.png". | ||
If not, do no thing. | ||
Only support 10.13+ | ||
|
||
## Build | ||
```shell | ||
$ swift build | ||
``` | ||
|
||
## Usage | ||
```shell | ||
$ ./clipboard | ||
``` | ||
|
||
## TODO | ||
- [ ] Judge clipboard content | ||
- [ ] support command line argvs, such as save-dir, output-name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// | ||
// main.swift | ||
// A simple command line tool to Paste PNG into files, much like pbpaste does for text. | ||
// | ||
// Created by PoplarYang on 2020/3/23. | ||
// Copyright © 2020 PoplarYang. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import AppKit | ||
|
||
class SaveManager{ | ||
|
||
let pasteboard = NSPasteboard.general | ||
|
||
@available(OSX 10.13, *) | ||
func saveToLocal(){ | ||
// .fileURL | ||
// if let data = pasteboard.data(forType: .fileURL){ | ||
// guard let fileUri = NSString(data: data , encoding: String.Encoding.utf8.rawValue) else{ | ||
// print("Cilpboard has file, but it can't be converted by system."); | ||
// return | ||
// } | ||
// | ||
// guard let url = URL(string: fileUri as String) else{ | ||
// print("Convert URL failed.") | ||
// return | ||
// } | ||
// print("File Type: \(url)") | ||
// | ||
// guard let contentData = try? Data(contentsOf: url) else{ | ||
// print("Read file failed, maybe not be permiteted.") | ||
// return | ||
// } | ||
// print("Content Count: \(contentData.count)") | ||
// process | ||
// return | ||
// } | ||
|
||
// Image PNG | ||
if let data = pasteboard.data(forType: .png){ | ||
let fileName: String = "image.png" | ||
// let pngPath = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent(filePath) | ||
let home = FileManager.default.homeDirectoryForCurrentUser | ||
let pngPath = home.appendingPathComponent(fileName) | ||
|
||
do { | ||
try data.write(to: pngPath, options: .atomic) | ||
} catch { | ||
print(error) | ||
} | ||
print("File Path: \(pngPath)") | ||
print("File Type: PNG") | ||
print("Content Count: \(data.count)") | ||
|
||
return | ||
} | ||
|
||
// File Content Type | ||
// if let data = pasteboard.data(forType: .fileContents){ | ||
// print("File Type: Content") | ||
// print("Content Count: \(data.count)") | ||
// process | ||
// return | ||
// } | ||
|
||
print("File Type: not png") | ||
exit(-1) | ||
} | ||
} | ||
|
||
let manager = SaveManager() | ||
if #available(OSX 10.13, *) { | ||
manager.saveToLocal() | ||
} else { | ||
// Fallback on earlier versions | ||
print("Only support 10.13+") | ||
exit(-1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import XCTest | ||
|
||
import clipboardTests | ||
|
||
var tests = [XCTestCaseEntry]() | ||
tests += clipboardTests.allTests() | ||
XCTMain(tests) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import XCTest | ||
|
||
#if !canImport(ObjectiveC) | ||
public func allTests() -> [XCTestCaseEntry] { | ||
return [ | ||
testCase(clipboardTests.allTests), | ||
] | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import XCTest | ||
import class Foundation.Bundle | ||
|
||
final class clipboardTests: XCTestCase { | ||
func testExample() throws { | ||
// This is an example of a functional test case. | ||
// Use XCTAssert and related functions to verify your tests produce the correct | ||
// results. | ||
|
||
// Some of the APIs that we use below are available in macOS 10.13 and above. | ||
guard #available(macOS 10.13, *) else { | ||
return | ||
} | ||
|
||
let fooBinary = productsDirectory.appendingPathComponent("clipboard") | ||
|
||
let process = Process() | ||
process.executableURL = fooBinary | ||
|
||
let pipe = Pipe() | ||
process.standardOutput = pipe | ||
|
||
try process.run() | ||
process.waitUntilExit() | ||
|
||
let data = pipe.fileHandleForReading.readDataToEndOfFile() | ||
let output = String(data: data, encoding: .utf8) | ||
|
||
XCTAssertEqual(output, "Hello, world!\n") | ||
} | ||
|
||
/// Returns path to the built products directory. | ||
var productsDirectory: URL { | ||
#if os(macOS) | ||
for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") { | ||
return bundle.bundleURL.deletingLastPathComponent() | ||
} | ||
fatalError("couldn't find the products directory") | ||
#else | ||
return Bundle.main.bundleURL | ||
#endif | ||
} | ||
|
||
static var allTests = [ | ||
("testExample", testExample), | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>CFBundleDevelopmentRegion</key> | ||
<string>en</string> | ||
<key>CFBundleExecutable</key> | ||
<string>$(EXECUTABLE_NAME)</string> | ||
<key>CFBundleIdentifier</key> | ||
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string> | ||
<key>CFBundleInfoDictionaryVersion</key> | ||
<string>6.0</string> | ||
<key>CFBundleName</key> | ||
<string>$(PRODUCT_NAME)</string> | ||
<key>CFBundlePackageType</key> | ||
<string>BNDL</string> | ||
<key>CFBundleShortVersionString</key> | ||
<string>1.0</string> | ||
<key>CFBundleSignature</key> | ||
<string>????</string> | ||
<key>CFBundleVersion</key> | ||
<string>$(CURRENT_PROJECT_VERSION)</string> | ||
<key>NSPrincipalClass</key> | ||
<string></string> | ||
</dict> | ||
</plist> |
Oops, something went wrong.