Skip to content

Commit 52e5f43

Browse files
MitnitskyCopilot
andcommitted
fix(macos): use setImageData for clipboard instead of setData/osascript
Two issues fixed: - setData("image/png") doesn't map to a macOS UTI, so native apps can't see the clipboard data. Use setImageData(QImage) which Qt converts to public.tiff. - Remove saveJpegToClipboardMacOS which used a slow osascript subprocess and had a format mismatch bug (wrote JPEG, read as PNGf). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent aecdc02 commit 52e5f43

1 file changed

Lines changed: 11 additions & 46 deletions

File tree

src/utils/screenshotsaver.cpp

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -111,45 +111,6 @@ QString ShowSaveFileDialog(const QString& title, const QString& directory)
111111
}
112112
}
113113

114-
void saveJpegToClipboardMacOS(const QPixmap& capture)
115-
{
116-
// Convert QPixmap to JPEG data
117-
QByteArray jpegData;
118-
QBuffer buffer(&jpegData);
119-
buffer.open(QIODevice::WriteOnly);
120-
121-
QImageWriter imageWriter(&buffer, "jpeg");
122-
123-
// Set JPEG quality to whatever is in settings
124-
imageWriter.setQuality(ConfigHandler().jpegQuality());
125-
if (!imageWriter.write(capture.toImage())) {
126-
qWarning() << "Failed to write image to JPEG format.";
127-
return;
128-
}
129-
130-
// Save JPEG data to a temporary file
131-
QTemporaryFile tempFile;
132-
if (!tempFile.open()) {
133-
qWarning() << "Failed to open temporary file for writing.";
134-
return;
135-
}
136-
tempFile.write(jpegData);
137-
tempFile.close();
138-
139-
// Use osascript to copy the contents of the file to clipboard
140-
QProcess process;
141-
QString script =
142-
QString("set the clipboard to (read (POSIX file \"%1\") as «class PNGf»)")
143-
.arg(tempFile.fileName());
144-
process.start("osascript", QStringList() << "-e" << script);
145-
if (!process.waitForFinished()) {
146-
qWarning() << "Failed to execute AppleScript.";
147-
}
148-
149-
// Clean up
150-
tempFile.remove();
151-
}
152-
153114
void saveToClipboardMime(const QPixmap& capture, const QString& imageType)
154115
{
155116
QByteArray array;
@@ -169,7 +130,12 @@ void saveToClipboardMime(const QPixmap& capture, const QString& imageType)
169130

170131
auto* mimeData = new QMimeData();
171132

172-
#ifdef USE_WAYLAND_CLIPBOARD
133+
#if defined(Q_OS_MACOS)
134+
// On macOS, setData("image/png") doesn't map to a UTI that native
135+
// apps recognize. Use setImageData which Qt converts to public.tiff.
136+
mimeData->setImageData(formattedPixmap.toImage());
137+
QApplication::clipboard()->setMimeData(mimeData);
138+
#elif defined(USE_WAYLAND_CLIPBOARD)
173139
if (QGuiApplication::platformName() == "wayland") {
174140
mimeData->setImageData(formattedPixmap.toImage());
175141
mimeData->setData(QStringLiteral("x-kde-force-image-copy"),
@@ -206,14 +172,13 @@ void saveToClipboard(const QPixmap& capture)
206172
AbstractLogger() << QObject::tr("Capture saved to clipboard.");
207173
}
208174
if (ConfigHandler().useJpgForClipboard()) {
209-
#ifdef Q_OS_MACOS
210-
saveJpegToClipboardMacOS(capture);
211-
#else
212175
saveToClipboardMime(capture, "jpeg");
213-
#endif
214176
} else {
215-
// Need to send message before copying to clipboard
216-
#if defined(Q_OS_LINUX) || defined(Q_OS_UNIX)
177+
#if defined(Q_OS_MACOS)
178+
// On macOS, setPixmap uses lazy clipboard which fails when the
179+
// process exits ("Cannot keep promise"). Use serialized bytes.
180+
saveToClipboardMime(capture, "png");
181+
#elif defined(Q_OS_LINUX) || defined(Q_OS_UNIX)
217182
if (DesktopInfo().waylandDetected()) {
218183
saveToClipboardMime(capture, "png");
219184
} else {

0 commit comments

Comments
 (0)