05月23, 2024

将IOS设备复制的文本粘贴至Windows电脑

流程大概是IOS将剪贴板内容通过服务器写到NAS中,再在Windows端运行程序,获取该文件内容写到剪切板中。

IOS端

新建一快捷指令,取得剪贴板内容,用"\n"替换掉换行符,然后通过ssh执行命令

echo -e "(快捷指令变量: 已更新的文字)" > /path/iosPaste && /path/pasteToNas.sh

服务器

服务器上pasteToNas.sh如下

#!/bin/bash

SOURCE_FILE_PATH="/path/iosPaste"
DESTINATION_FILE_PATH="/path/iosPaste"

SFTP_HOST=***
SFTP_USER=***
SFTP_PORT=***

# 使用sftp命令传输文件
sftp -P ${SFTP_PORT} ${SFTP_USER}@${SFTP_HOST} <<EOF
put -r ${SOURCE_FILE_PATH} ${DESTINATION_FILE_PATH}
exit
EOF

Windows端

#include <windows.h>
#include <stdio.h>

void SetClipboardText(const char *text);

int main() {
    FILE *file;
    char buffer[1000];
    char content[10000] = "";

    file = fopen("path/iosPaste", "r");
    if (file == NULL) {
        printf("Error opening the file.\n");
        return 1;
    }

    while (fgets(buffer, sizeof(buffer), file) != NULL) {
        strcat(content, buffer);
    }

    fclose(file);

    SetClipboardText(content);

    printf("File content has been copied to clipboard.\n");

    return 0;
}

void SetClipboardText(const char *text) {
    HGLOBAL hMem;
    char *pMem;

    if (!OpenClipboard(NULL))
        return;

    EmptyClipboard();

    hMem = GlobalAlloc(GMEM_MOVEABLE, strlen(text) + 1);
    if (hMem == NULL) {
        CloseClipboard();
        return;
    }

    pMem = (char*)GlobalLock(hMem);
    strcpy(pMem, text);
    GlobalUnlock(hMem);

    SetClipboardData(CF_TEXT, hMem);

    CloseClipboard();
}

或者直接cmd

clip < /path/iosPaste

尝试自动复制到剪切板,失败

github更新PowerShell,将ssh默认终端改为Powershell7

New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Program Files\PowerShell\7\pwsh.exe" -PropertyType String -Force

ssh连接后使用下面语句就可以将文本写入到本机(https://github.com/PowerShell/PowerShell/issues/18116)

$text="text_to_copy"
Write-Host "`e]52;;$([System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($text)))`a"

尝试了在NAS中将文本写到ps1文件中,ssh电脑执行,但失败,应该是因为OSC52需要终端

echo '$text=@"' > /path/iosPaste1.ps1
type /path/iosPaste >> /path/iosPaste1.ps1
echo '"@' >> /path/iosPaste1.ps1
echo '' >> /path/iosPaste1.ps1
echo 'Write-Host "`e]52;;$([System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($text)))`a"' >> /path/iosPaste1.ps1
type /path/iosPaste1.ps1 | ssh ***.***.***.*** -l ***
#& "C:\Program Files (x86)\WinSCP\WinSCP.com" `
#    /command `
#    "open sftp://***@***.***.***.***" `
#    "put ""/path/iosPaste1.ps1"" ""/path/temp.ps1""" `
#    "exit"
#ssh ***.***.***.*** -l *** 'powershell -ExecutionPolicy Bypass -File /path/temp.ps1'

本文链接:http://blog.zireaels.com/post/ios_paste.html

-- EOF --

Comments