Skip to content

Commit

Permalink
demo available
Browse files Browse the repository at this point in the history
  • Loading branch information
PoplarYang committed Mar 24, 2020
1 parent 8a3d18f commit 26305ac
Show file tree
Hide file tree
Showing 13 changed files with 876 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Package.swift
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"]),
]
)
21 changes: 21 additions & 0 deletions README.md
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
79 changes: 79 additions & 0 deletions Sources/clipboard/main.swift
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)
}
7 changes: 7 additions & 0 deletions Tests/LinuxMain.swift
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)
9 changes: 9 additions & 0 deletions Tests/clipboardTests/XCTestManifests.swift
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
47 changes: 47 additions & 0 deletions Tests/clipboardTests/clipboardTests.swift
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),
]
}
25 changes: 25 additions & 0 deletions clipboard.xcodeproj/clipboardTests_Info.plist
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>
Loading

0 comments on commit 26305ac

Please sign in to comment.