I would like to mark a file extension as "Safe to open from the Internet".
To do so I need to create a preferences file ~/Library/Preferences/com.apple.DownloadAssessment.plist and add the extension to that.
If the file already exists, I need to preserve its existing settings, so simply overwriting the file is not an option.
I tried to use QSettings to edit it like this:
void MainWindow
::_markExtensionSafe
(const QString &ext
) {
qDebug() << "Marking extension" << ext << "as safe to open instead of download";
QString key
= "LSRiskCategorySafe/LSRiskCategoryExtensions";
QVariant currentSetting
= plist.
value(key
);
if ( !exts.contains(ext) ) /* add extension if it doesn't exists already */
{
exts << ext;
plist.setValue(key, exts);
}
}
void MainWindow::_markExtensionSafe(const QString &ext)
{
qDebug() << "Marking extension" << ext << "as safe to open instead of download";
QString filename = QDesktopServices::storageLocation(QDesktopServices::HomeLocation)+"/Library/Preferences/com.apple.DownloadAssessment.plist";
QString key = "LSRiskCategorySafe/LSRiskCategoryExtensions";
QSettings plist(filename, QSettings::NativeFormat);
QVariant currentSetting = plist.value(key);
QStringList exts = currentSetting.toStringList();
if ( !exts.contains(ext) ) /* add extension if it doesn't exists already */
{
exts << ext;
plist.setValue(key, exts);
}
}
To copy to clipboard, switch view to plain text mode
But it generates:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>LSRiskCategorySafe.LSRiskCategoryExtensions</key>
<array>
<string>myextension</string>
</array>
</dict>
</plist>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>LSRiskCategorySafe.LSRiskCategoryExtensions</key>
<array>
<string>myextension</string>
</array>
</dict>
</plist>
To copy to clipboard, switch view to plain text mode
While the format supposedly has to look like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>LSRiskCategorySafe</key>
<dict>
<key>LSRiskCategoryExtensions</key>
<array>
<string>myextension</string>
</array>
</dict>
</dict>
</plist>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>LSRiskCategorySafe</key>
<dict>
<key>LSRiskCategoryExtensions</key>
<array>
<string>myextension</string>
</array>
</dict>
</dict>
</plist>
To copy to clipboard, switch view to plain text mode
Is there a way to tell QSettings to create proper subkeys?
Or am I better off editing the file using the XML functions instead?
Added after 1 20 minutes:
Also tried this:
void MainWindow
::_markExtensionSafe
(const QString &ext
) {
qDebug() << "Marking extension" << ext << "as safe to open instead of download";
QString key
= "LSRiskCategorySafe";
QString subkey
= "LSRiskCategoryExtensions";
QVariantMap map = plist.value(key).toMap();
if ( !exts.contains(ext) ) /* add extension if it doesn't exists already */
{
exts << ext;
map[subkey] = exts;
plist.setValue(key, map);
}
}
void MainWindow::_markExtensionSafe(const QString &ext)
{
qDebug() << "Marking extension" << ext << "as safe to open instead of download";
QString filename = QDesktopServices::storageLocation(QDesktopServices::HomeLocation)+"/Library/Preferences/com.apple.DownloadAssessment.plist";
QString key = "LSRiskCategorySafe";
QString subkey = "LSRiskCategoryExtensions";
QSettings plist(filename, QSettings::NativeFormat);
QVariantMap map = plist.value(key).toMap();
QStringList exts = map.value(subkey).toStringList();
if ( !exts.contains(ext) ) /* add extension if it doesn't exists already */
{
exts << ext;
map[subkey] = exts;
plist.setValue(key, map);
}
}
To copy to clipboard, switch view to plain text mode
But then it generates:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>LSRiskCategorySafe</key>
<dict>
<key>LSRiskCategoryExtensions</key>
<array>
<array>
<string>myextension</string>
</array>
</array>
</dict>
</dict>
</plist>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>LSRiskCategorySafe</key>
<dict>
<key>LSRiskCategoryExtensions</key>
<array>
<array>
<string>myextension</string>
</array>
</array>
</dict>
</dict>
</plist>
To copy to clipboard, switch view to plain text mode
Don't understand why it generates <array> two times.
Bookmarks