Skip to content

Commit

Permalink
v0.0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
alex.wei committed Apr 23, 2019
1 parent 70c2303 commit de066ba
Show file tree
Hide file tree
Showing 13 changed files with 282 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
build
lib/binding
npm-debug.log
yarn-error.log
package-lock.json
.DS_Store
node_modules
6 changes: 6 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/build
/node_modules
/npm-debug.log
/lib/binding
yarn.lock
npm-debug.log
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 zman

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# clipboard-files

A nodejs addon, read file path from clipboard, support win32 and mac osx.

## build for nwjs

```shell
npm i clipboard-files --target=0.14.0 --target-arch=ia32
```
## build for electron

```shell
npm i clipboard-files --target=2.0.5 --target-arch=ia32
```

## build for nodejs

```shell
npm i clipboard-files
```

## example

```js
const clipboard = require('clipboard-files')
let fileNames = clipboard.readFiles()
```
58 changes: 58 additions & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"targets": [{
"target_name": "binding",
"sources": [
"src/main.cc"
],
"include_dirs": ["<!(node -e \"require('nan')\")"],
"conditions": [
['OS=="mac"',
{
"sources":[
"src/clip_osx.h",
"src/clip_osx.mm"
],
'defines': [
'__MACOSX_CORE__',
'__MAC__'
],
'link_settings': {
'libraries': [
'-framework Cocoa',
'-framework CoreFoundation',
]
},
'xcode_settings': {
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
'OTHER_CFLAGS': [
'-ObjC++',
'-std=c++11'
]
}
}
],
['OS=="win"',
{
"sources":[
"src/clip_win.h",
"src/clip_win.cc"
],
'defines': [
'__WIN32__'
]
}
],
['OS=="linux"',
{}
]
]
}, {
"target_name": "action_after_build",
"type": "none",
"dependencies": ["<(module_name)"],
"copies": [{
"files": [ "<(PRODUCT_DIR)/<(module_name).node" ],
"destination": "<(module_path)"
}]
}]
}
5 changes: 5 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var binary = require('node-pre-gyp');
var path = require('path');
var binding_path = binary.find(path.resolve(path.join(__dirname,'../package.json')));
var binding = require(binding_path);
module.exports = exports = binding;
38 changes: 38 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "clipboard-files",
"version": "0.0.7",
"description": "A nodejs addon, read file path from clipboard, support win32 and mac osx",
"main": "./lib/index.js",
"directories": {
"lib": "lib",
"test": "test"
},
"binary": {
"module_name": "binding",
"module_path": "./lib/binding/{node_abi}-{platform}-{arch}/",
"package_name": "{node_abi}-{platform}-{arch}.tar.gz",
"host": "https://github.com/alex8088/clipboard-files/releases/download/",
"remote_path": "v{version}"
},
"keywords": [
"clipboard",
"files",
"nwjs",
"electron"
],
"repository": {
"type": "git",
"url": "https://github.com/alex8088/clipboard-files.git"
},
"scripts": {
"test": "node ./test",
"install": "node-pre-gyp install --fallback-to-build"
},
"author": "alex.wei",
"license": "MIT",
"gypfile": true,
"dependencies": {
"nan": "^2.13.2",
"node-pre-gyp": "^0.12.0"
}
}
13 changes: 13 additions & 0 deletions src/clip_osx.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <Cocoa/Cocoa.h>
#include <string>
#include <node.h>
using namespace std;
using v8::Array;
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;

Local<Array> get_file_names(Isolate *isolate);
25 changes: 25 additions & 0 deletions src/clip_osx.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "clip_osx.h"

Local<Array> get_file_names(Isolate *isolate){
NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
NSArray* tempArray = [pasteboard pasteboardItems];
int count = 0;
for(NSPasteboardItem *tmpItem in tempArray){
NSString *pathString = [tmpItem stringForType:@"public.file-url"];
const char* str = [pathString UTF8String];
if(str){
count++;
}
}
Local<Array> fileNames = Array::New(isolate, count);
count =0;
for(NSPasteboardItem *tmpItem in tempArray){
NSString *pathString = [tmpItem stringForType:@"public.file-url"];
const char* str = [pathString UTF8String];
if(str){
fileNames->Set(count,String::NewFromUtf8(isolate,str));
count++;
}
}
return fileNames;
}
38 changes: 38 additions & 0 deletions src/clip_win.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "clip_win.h"

char *GBK2Utf8(const char *strGBK)
{
WCHAR *str1;
int n = MultiByteToWideChar(CP_ACP, 0, strGBK, -1, NULL, 0);
str1 = new WCHAR[n];
MultiByteToWideChar(CP_ACP, 0, strGBK, -1, str1, n);
n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL);
char *str2 = new char[n];
WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, n, NULL, NULL);
delete[] str1;
str1 = NULL;
return str2;
}

Local<Array> get_file_names(Isolate *isolate)
{
Local<Array> fileNames = Array::New(isolate, 0);
if (OpenClipboard(NULL)) // open clipboard
{
HDROP hDrop = HDROP(::GetClipboardData(CF_HDROP)); // get the file path hwnd of clipboard
if (hDrop != NULL)
{
char szFilePathName[MAX_PATH + 1] = { 0 };
UINT nNumOfFiles = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0); // get the count of files
fileNames = Array::New(isolate, nNumOfFiles);
for (UINT nIndex = 0; nIndex < nNumOfFiles; ++nIndex)
{
memset(szFilePathName, 0, MAX_PATH + 1);
DragQueryFile(hDrop, nIndex, szFilePathName, MAX_PATH); // get file name
fileNames->Set(nIndex, String::NewFromUtf8(isolate, GBK2Utf8(szFilePathName)));
}
}
CloseClipboard(); // close clipboard
}
return fileNames;
}
12 changes: 12 additions & 0 deletions src/clip_win.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <windows.h>
#include <node.h>

using v8::Array;
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;

Local<Array> get_file_names(Isolate *isolate);
30 changes: 30 additions & 0 deletions src/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <node.h>
#ifdef __WIN32__
#include "clip_win.h"
#elif __APPLE__
#include "clip_osx.h"
#endif

namespace clipboard
{
using v8::Array;
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;

void readFiles(const FunctionCallbackInfo<Value> &args)
{
Isolate *isolate = args.GetIsolate();
Local<Array> fileNames = get_file_names(isolate);
args.GetReturnValue().Set(fileNames);
}
void Init(Local<Object> exports)
{
NODE_SET_METHOD(exports, "readFiles", readFiles);
}

NODE_MODULE(NODE_GYP_MODULE_NAME, Init)
}
2 changes: 2 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const clipboard = require("../lib");
console.log("files:", clipboard.readFiles());

0 comments on commit de066ba

Please sign in to comment.