Jay_Miao
Jay_Miao

Reputation: 70

Can qt on Mac open finder and select(highlight) some files in the opened finder window?

On mac, my application creates some files in a folder for user, then I need to reveal the files to user. I can open the folder with finder by QDesktopServices::openUrl, but I can not select(highlight) the files just created in the opened finder window.

I have tried the follow code:

QStringList scriptArgs;
scriptArgs << QLatin1String("-e")
           << QString::fromLatin1("tell application \"Finder\" to select POSIX file \"%1\"")
                                     .arg(filePath);
QProcess::execute(QLatin1String("/usr/bin/osascript"), scriptArgs);

scriptArgs.clear();
scriptArgs << QLatin1String("-e")
           << QLatin1String("tell application \"Finder\" to activate");
QProcess::execute("/usr/bin/osascript", scriptArgs);`

... but only one file can be selected.

Someone help me? Thank you!

Upvotes: 1

Views: 371

Answers (2)

Jay_Miao
Jay_Miao

Reputation: 70

I have completed it with:

    NSMutableArray *fileURLs = [NSMutableArray arrayWithCapacity:fileList.size()];

for(int i=0; i<fileList.size(); i++)
{
    [fileURLs addObject:[NSURL fileURLWithPath:[NSString stringWithCString:fileList.at(i).toUtf8().data() encoding:4] isDirectory:false]];
}

[[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs:fileURLs];

Upvotes: 1

E4z9
E4z9

Reputation: 1767

You can tell Finder to select multiple items by passing select a list of objects, for example

tell application "Finder" to select {POSIX file "/Applications/Calendar.app", POSIX file "/Applications/Contacts.app"}

(You can play around with that kind of scripts using /Applications/Utilities/Script Editor.app.)

Upvotes: 0

Related Questions