Last active
July 22, 2019 14:05
-
-
Save u27a4/ea9a726932948f548166307b6813e6c5 to your computer and use it in GitHub Desktop.
aescript; replace selected item names.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 入力用ダイアログの表示 | |
function showPrompt(settings, selection) { | |
var ui = new Window("""dialog { | |
orientation: 'column', | |
body: Panel { | |
orientation: 'column', | |
alignChildren:'right', | |
pattern: Group { | |
orientation: 'row', | |
_: StaticText { text: 'Find what:' }, | |
field: EditText { characters: 15 }, | |
}, | |
replace: Group { | |
orientation: 'row', | |
_: StaticText { text: 'Replace with:' }, | |
field: EditText { characters: 15 }, | |
}, | |
composition: Group { | |
orientation: 'row', | |
_: StaticText { text: 'Item type:' }, | |
field: Checkbox { text: 'Composition', characters: 14 }, | |
}, | |
footage: Group { | |
orientation: 'row', | |
field: Checkbox { text: 'Footage', characters: 14 }, | |
}, | |
folder: Group { | |
orientation: 'row', | |
field: Checkbox { text: 'Folder', characters: 14 }, | |
}, | |
}, | |
footer: Group { | |
orientation: 'row', | |
alignment: 'right', | |
previewButton: Button { text: 'Preview' }, | |
okButton: Button { text: 'OK', properties: { name: 'ok' } }, | |
}, | |
}""", "Replace Item Names"); | |
ui.ctrls = { | |
pattern: ui.body.pattern.field, | |
replace: ui.body.replace.field, | |
composition: ui.body.composition.field, | |
footage: ui.body.footage.field, | |
folder: ui.body.folder.field, | |
previewButton: ui.footer.previewButton, | |
okButton: ui.footer.okButton, | |
}; | |
ui.funcs = { | |
// 置換対象アイテムを探す | |
findTargets: function () { | |
var targets = []; | |
var pattern = ui.ctrls.pattern.text; | |
if (pattern && pattern.length > 0) { | |
for (var i = 0; i < selection.length; i++) { | |
var item = selection[i]; | |
if (item.name.indexOf(pattern) === -1) continue; | |
if (item instanceof CompItem && ui.ctrls.composition.value === false) continue; | |
if (item instanceof FootageItem && ui.ctrls.footage.value === false) continue; | |
if (item instanceof FolderItem && ui.ctrls.folder.value === false) continue; | |
targets.push(item); | |
} | |
} | |
return targets; | |
}, | |
}; | |
// 検索語や対象が変更されたときの処理 | |
ui.ctrls.pattern.onChanging = | |
ui.ctrls.composition.onClick = | |
ui.ctrls.footage.onClick = | |
ui.ctrls.folder.onClick = function () { | |
var targets = ui.funcs.findTargets(); | |
ui.ctrls.okButton.enabled = targets.length > 0; | |
ui.ctrls.previewButton.enabled = targets.length > 0; | |
} | |
// 「プレビュー」が押下されたときの処理 | |
ui.ctrls.previewButton.onClick = function () { | |
var targets = ui.funcs.findTargets(); | |
showPreview(targets, ui.ctrls.pattern.text, ui.ctrls.replace.text); | |
} | |
// 「OK」が押下されたときの処理 | |
ui.ctrls.okButton.onClick = function () { | |
var targets = ui.funcs.findTargets(); | |
ui.result = targets.length === 0 ? null : { | |
pattern: ui.ctrls.pattern.text, | |
replace: ui.ctrls.replace.text, | |
composition: ui.ctrls.composition.value, | |
footage: ui.ctrls.footage.value, | |
folder: ui.ctrls.folder.value, | |
targets: targets, | |
}; | |
ui.close(); | |
} | |
// 選択中アイテムの集計 | |
var counts = { numComps: 0, numFootages: 0, numFolders: 0 }; | |
for (var i = 0; i < selection.length; i++) { | |
if (selection[i] instanceof CompItem) counts.numComps++; | |
if (selection[i] instanceof FootageItem) counts.numFootages++; | |
if (selection[i] instanceof FolderItem) counts.numFolders++; | |
} | |
// 初期値の代入 | |
ui.result = null; | |
ui.ctrls.pattern.text = settings.pattern; | |
ui.ctrls.replace.text = settings.replace; | |
ui.ctrls.composition.value = settings.composition === 'true'; | |
ui.ctrls.footage.value = settings.footage === 'true'; | |
ui.ctrls.folder.value = settings.folder === 'true'; | |
ui.ctrls.composition.enabled = counts.numComps > 0; | |
ui.ctrls.footage.enabled = counts.numFootages > 0; | |
ui.ctrls.folder.enabled = counts.numFolders > 0; | |
ui.ctrls.pattern.onChanging(); | |
// 表示 | |
ui.center(); | |
ui.show(); | |
return ui.result; | |
} | |
// プレビュー用ダイアログの表示 | |
function showPreview(items, find, replace) { | |
var ui = new Window("""dialog { | |
orientation: 'column', | |
preferredSize: [, 400], | |
properties: { resizeable: true }, | |
body: Panel { | |
alignment: ['fill', 'fill'], | |
lines: ListBox { | |
alignment: ['fill', 'fill'], | |
properties: { | |
showHeaders: true, | |
numberOfColumns: 4, | |
multiselect: true, | |
columnTitles: ['#', 'Type', 'Before', 'After'], | |
columnWidths: [10, 100, 300, 300], | |
}, | |
}, | |
}, | |
}""", "Replace Item Names (Preview)"); | |
// アイテム名の登録 | |
for (var i = 0; i < items.length; i++) | |
{ | |
var item = ui.body.lines.add('item', i + 1); | |
item.subItems[0].text = items[i].typeName; | |
item.subItems[1].text = items[i].name; | |
item.subItems[2].text = items[i].name.replace(find, replace); | |
} | |
ui.onResizing = ui.onResize = function () { this.layout.resize(); }; | |
ui.center(); | |
ui.show(); | |
} | |
// 設定の読み込み | |
function loadSettings(sectionName, valueObject) { | |
for (var keyName in valueObject) { | |
valueObject[keyName] = app.settings.haveSetting(sectionName, keyName) ? app.settings.getSetting(sectionName, keyName) : valueObject[keyName]; | |
} | |
return valueObject; | |
} | |
// 設定の保存 | |
function saveSettings(sectionName, valueObject) { | |
for (var keyName in valueObject) { | |
app.settings.saveSetting(sectionName, keyName, valueObject[keyName]); | |
} | |
} | |
// 選択中のアイテムをすべて取得 | |
function getSelection() { | |
var items = [].concat(app.project.selection); | |
var result = []; | |
var collected = {}; | |
while (items.length > 0) { | |
var item = items.pop(); | |
for (var i = 1; i <= item.numItems || 0; i++) { | |
if (item.id in collected === false) { | |
items.push(item.items[i]); | |
} | |
} | |
collected[item.id] = item; | |
} | |
for (var itemId in collected) { | |
result.push(collected[itemId]); | |
} | |
return result; | |
} | |
// アイテム名の置換 | |
function replaceItemName(items, find, replace) { | |
for (var i = 0; i < items.length; i++) { | |
items[i].name = items[i].name.replace(find, replace); | |
} | |
} | |
// main | |
{ | |
var kSectionName = 'Replace Item Names Preference Section'; | |
var settings = loadSettings(kSectionName, { | |
pattern: '', | |
replace: '', | |
composition: 'true', | |
footage: 'false', | |
folder: 'true', | |
}); | |
var result = showPrompt(settings, getSelection()); | |
if (result) | |
{ | |
app.beginUndoGroup('Replace Item Names'); | |
replaceItemName(result.targets, result.pattern, result.replace); | |
app.endUndoGroup(); | |
saveSettings(kSectionName, result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment