ファンシープログレッシブ!(め_め)
InDesign ではすべての属性がスクリプト可能なのかと思いきや、フレームグリッドの基本情報である文字数/行数だけはなぜか簡単に読み書きする方法が用意されてない。日本語圏では頻繁に操作する箇所なので、自分でメソッドを作った。
などの設定をすべて考慮して、テキストフレームの geometricBounds から文字数と行数を逆算する仕組み。
TextFrame.prototype の拡張になっているので、外部ファイル扱い (.jsxinc) にしておいて #import で適宜読み込むのがおすすめ。
Getter と setter を兼ねた gridDimensions() というメソッドになっている。そのまま呼び出せば get、引数を渡して呼び出せば set として動作する。
フレームの種類が「フレームグリッド」に設定されているテキストフレームにのみ有効。フレームグリッドでない場合はエラーを返す。
1段のサイズが 15文字 × 20行 のテキストフレーム myTextFrame があるとすると、
myTextFrame.gridDimensions();
で [15, 20] という [文字数, 行数] 形式の array を返す。
同じく [文字数, 行数] 形式の array を渡すことで、フレームグリッドのサイズを変更する。
myTextFrame.gridDimensions([16, 40]);
これで、段数・字間などはそのままに、各段のサイズが 16文字 × 40行 に変更される。
どちらか片方だけ変更したい場合は、[0, 55] のようにゼロを使えばそこだけ無変更。
みずほ銀行のネットバンキング(みずほダイレクト)で振込手続きをするときに、第2暗証番号を自動的に入力してくれるブックマークレット。
javascript:(function(){var%20e=[1,2,3,4,5,6];var%20f=document.getElementById('bodycontent').textContent;var%20g=f.match(/\d(?=番目)/g);function%20gia(){var%20a=['Anshu2','Anshu2_2','Anshu2_3','Anshu2_4'];var%20b;var%20c=[];for(var%20i=0;i
使い方
スクリプト冒頭に 1,2,3,4,5,6 とあるところを、同じようにカンマ区切りにした自分の第2暗証番号に書きかえる。
ブックマークとして保存。
振込手続きの最後に第2暗証番号を入力する画面が表示されたら、ブックマークをクリック。
暗証番号が 123456 だった場合の自動入力例
指定された順番どおりに暗証番号が入力される。
注意
パソコンに平文で暗証番号を保存することになります。この暗証番号とは別にログイン時に2〜3段階の認証があるものの、セキュリティ的なことは自己責任でお願いします。最低限、ブックマークの保存名を「みずほ暗証番号」などのわかりやすい名前にしないほうがいいでしょう。
選択されてる入力モードが英語のときにだけ実行してほしい処理があれば、こういうスクリプトでテストできる。
例えば Typinator でブラケット [] の auto-pairing を普通に設定すると、日本語モードでカギ括弧を打ってるときにまで自動補完してきて困る。そういうときに入力モードの判定スクリプトを挟むと、英語モードのときだけ自動補完してくれるように設定可能。
property true_if_input : "U.S." -- input mode name to test for
tell application "System Events"
tell process "SystemUIServer"
set current_input to value of first menu bar item of menu bar 1 whose description is "text input"
end tell
end tell
if current_input is true_if_input then
return true
else
return false
end if
※ GUIスクリプティングなので、システム環境設定 > ユニバーサルアクセス > 補助装置にアクセスできるようにする がオンになっている必要がある
欧米ヒットソングの完コピ音源を「カバー」と称して iTunes Store などで販売している例がある。権利者の許可が下りているとも思えない怪しい商品がなぜ野放しにされているのかわからないが、とにかく面白い。
そんな露骨なパチモンに騙される客なんてそうそういないだろうと思った人は、これを見てほしい。
ダンス部門(左)と総合(右)のアルバムチャート
「24 Hour Party Project」という企画名の偽コンピレーションがしっかりトップ10入りしている。
さらに意味のわからないことに、買った客が特に文句を言ってない。
偽アルバムの高評価
この時点ではまだ、「パチモンとわかったうえで、600円というお手頃価格に満足して買っているのかもしれない」と思っていた。中国の怪しい市場で3000円のヴィトンを買うのと同じに違いない、と。
だがこのシングルチャートはどうだろう。
シングルチャート(ダンス部門)
なんとここにもパチモンが2曲入っている。1曲150円というまったく同じ価格で本物が売っているのだから、「ケチって安いほうを買った」説はもう通用しない。面白がってわざわざ偽物を買ったのでなければ(実際に私も面白がって買うかもしれない)、騙されて買ったことになる。本物のほうはダンス部門ではなくポップス部門に登録されているため、探す場所を間違えればそういうミスもあるだろう。問題はそのあとだ。買ったあとに「騙された」と気づくならともかく、気づいてない。「どっちでもいい」のだ!
偽物のほうを試聴してみれば納得すると思う。原曲を聴き慣れていたり、もともとボーカルの声質を把握していればさすがに違いがわかるが、一度や二度どこかで耳にしたくらいでは違和感をおぼえない気もする。騙された購入者の勘の悪さだとか、偽物の完成度がどうとかいう話ではない。決定的に「どっちでもいいレベル」の音楽であることが証明されてしまった。
データファイルがまるごと複製されたならともかく、ボーカルやトラックまで含めた音楽自体がいとも簡単に模造され、どっちでもいいリスナーに買われていく。もともとその程度の音楽を、オーディオデータとして売るビジネスにどれほど無理があるか。ファイルの違法コピーなら堂々と規制している業界側も、ここまで平然と売れてしまう完コピ音源に関しては1、取り締まるにしても恥ずかしすぎるのかもしれない。
スクリプト本体の実行前にユーザ設定値を変更しておき、本体実行後に元に戻す、という処理が頻出する。
特によくあるのが app.scriptPreferences の設定。 enableRedraw はほとんどの場合で disable したいだろうし、 measurementUnit は意図しない設定になっていると絶対値の組み込まれたアルゴリズムが不具合を起こす。
この辺の〈保持→変更→復元〉という処理を main() などに直接入れちゃうと煩雑なので、 scriptSettings.alter() と scriptSettings.revert() で本体ルーチンを挟むだけでいいようにメソッド化してみた。
// Make alter and revert methods
var scriptSettings = {
alter: function() {
var prefs = app.scriptPreferences;
var propStrs = ["enableRedraw", "measurementUnit", "userInteractionLevel"];
var tempVals = [false, AutoEnum.AUTO_VALUE, UserInteractionLevels.INTERACT_WITH_ALL]
var originalVals = [];
for (var i = 0; i
以上を本体処理より前に declare しておくだけ。
メリット
- 変更不要のプロパティがあれば、
propStrs と tempVals の array から削るだけ
- エラーなどで途中
exit() する場面でも、その手前で scriptSettings.revert() を call するだけ
scriptSettings の declare 時ではなく、 alter() を call した時点での設定値が保存される、という点にだけ注意。
iTunes を play/pause すると同時に音量をフェードイン/フェードアウトする AppleScript。
property resolution : 10 (* Increase for smoother fades. Default: 10 *)
property delayIncr : 0.2 (* Decrease for faster fades. Default: 0.2 *)
tell application "iTunes"
set originalVol to sound volume
set volIncr to originalVol div resolution
if player state is not playing then
set sound volume to 0
play
-- Fade in
repeat while (sound volume ≤ (originalVol - volIncr))
set sound volume to (sound volume + volIncr)
delay delayIncr
end repeat
else
-- Fade out
repeat while (sound volume > 0)
set sound volume to (sound volume - volIncr)
delay delayIncr
end repeat
pause
end if
set sound volume to originalVol
end tell
AirPlay 時代のエチケット。ステレオコンポのときから、ボリュームつまみをゆっくり絞ったあとに停止ボタンを押してたタイプの人用。
Spark や Quicksilver のようなツールにスクリプトを登録しておいて、ホットキーで呼び出す。たとえば F8 に設定しておくと、 F8 がフェード付きの iTunes 再生/停止キーになる。
Here’s a nice AppleScript I use to rewind or skip 30 seconds in iTunes, but only when the current track is a podcast or audiobook.
This allows you to have a single keyboard shortcut that works as a normal “previous track” key when listening to music, but automatically switches to a 30-second rewind when listening to podcasts, audiobooks, or whatever else you specify.
property skip_secs : 30 -- use negative value to rewind, positive value to skip ahead
property genre_list : {"Podcast", "Books & Spoken"}
tell application "iTunes"
if (exists of current track) is false then
return
end if
if genre of current track is in genre_list then
set target_time to (player position + skip_secs)
if (target_time > finish of current track) then
next track
else if (target_time
Options
Change the skip_secs property to your preferred interval in seconds. For example, setting skip_secs to 30 will result in a 30-sec fast forward, while setting it to -15 will make it a 15-sec rewind.
If you would like more kinds of tracks to respond to this skip behavior, add the genre names of those tracks to genre_list.
When you need to validate user input in an edittext field, it’s not enough to do so in its onChange event — you must also validate it in the dialog’s onClose. It’s the only way to stop the dialog from closing after a click on the OK button, so that the user has a chance to correct his input. But what if that close event occurred because of an explicit user action to simply dismiss the dialog, like clicking the Cancel button?
By default, it seems that the close event does not distinguish between an “OK” close and a “Cancel” close. This can be problematic in input validations, because if your validator is designed to prevent the dialog from closing upon detection of faulty input, it should absolutely not do so in the event of an explicit “Cancel” (or Esc) close.
Register event handlers for the Cancel button and Escape key, so that a justExit flag can be flipped when they occur. Then, the onClose handler can check the flag to determine whether it needs to validate and obstruct, or just get out of the way.
So first, define the boolean, and maybe a flagging function to flip it:
var justExitFlag = false;
function flipJustExit() {
justExitFlag = true;
}
Now add event listeners. Specifically, I guess there are three events you’ll want to monitor:
an Esc key press within the dialog window
myDialog.addEventListener("keydown", function (k) {
if (k.keyIdentifier == "U+001B" /* Escape key */) {
flipJustExit();
}
});a click on the Cancel button
myCancelBtn.addEventListener("click", flipJustExit);a Spacebar key press on the Cancel button
myCancelBtn.addEventListener("keydown", function (k) {
if (k.keyIdentifier == "U+0020" /* space key */) {
flipJustExit();
}
});Finally, your onClose handler might be something like this:
function validateInput() {
if (justExitFlag) {
return true; /* doesn’t stop dialog from closing */
} else if (inputIsInvalid()) {
alertUserWithCrazyPopup();
return false; /* stops dialog from closing */
}
}Here is a set of three utility functions I use when debugging my InDesign scripts, to ensure that they work correctly in environments with different measurement units (points, millimeters, etc.) and ruler origins.
If you haven’t already, read why managing measurement units is important.
Use this to display a quick alert of what the current settings are.
I often find that some of these settings get messed up at one point or another in a scripting session full of stepping, incomplete functions, or exiting mid-script. When things get weird, I check these settings.
Randomize the measurementUnit settings with this function to create the most implausible environment ever. (Horizontal agates and vertical picas, anyone?) Then, check to see if your script works correctly. Randomize and check again. Two or three cycles of this is probably enough to see if your script fails to work around measurementUnit diversity.
After your tampering is done, call this function to hit the reset button. The default values assigned in this function are mine (i.e., standard settings for a Japanese language user), so be sure to substitute in your own defaults.
#target indesign
var sp = app.scriptPreferences;
var vp = app.documents[0].isValid ?
document.viewPreferences : app.viewPreferences;
function showCurrentSettings() {
alert(
"Current settings:n" +
"Script: " + sp.measurementUnit.toString() + "nn" +
"Horizontal: " + vp.horizontalMeasurementUnits.toString() + "n" +
"Vertical: " + vp.verticalMeasurementUnits.toString() + "n" +
"Stroke: " + vp.strokeMeasurementUnits.toString() + "nn" +
"Text: " + vp.textSizeMeasurementUnits.toString() + "n" +
"Typo: " + vp.typographicMeasurementUnits.toString() + "nn" +
"Ruler: " + vp.rulerOrigin.toString()
);
}
function randomizeSettings() {
var unitArray = [
MeasurementUnits.POINTS,
MeasurementUnits.PICAS,
MeasurementUnits.INCHES,
MeasurementUnits.INCHES_DECIMAL,
MeasurementUnits.MILLIMETERS,
MeasurementUnits.CENTIMETERS,
MeasurementUnits.CICEROS,
MeasurementUnits.CUSTOM,
MeasurementUnits.AGATES,
MeasurementUnits.U,
MeasurementUnits.BAI,
MeasurementUnits.MILS,
MeasurementUnits.PIXELS,
MeasurementUnits.HA,
MeasurementUnits.AMERICAN_POINTS,
];
var scriptUnitArray = unitArray.concat(AutoEnum.AUTO_VALUE);
var strokeUnitArray = [
MeasurementUnits.POINTS,
MeasurementUnits.MILLIMETERS,
MeasurementUnits.PIXELS,
];
var textUnitArray = [
MeasurementUnits.POINTS,
MeasurementUnits.PIXELS,
MeasurementUnits.Q,
MeasurementUnits.AMERICAN_POINTS,
];
var typoUnitArray = [
MeasurementUnits.POINTS,
MeasurementUnits.PIXELS,
MeasurementUnits.HA,
MeasurementUnits.AMERICAN_POINTS,
MeasurementUnits.U,
MeasurementUnits.BAI,
MeasurementUnits.MILS,
];
var rulerOriginArray = [
RulerOrigin.SPREAD_ORIGIN,
RulerOrigin.PAGE_ORIGIN,
RulerOrigin.SPINE_ORIGIN,
];
function getRandomItem(myArray) {
var myIndex = Math.floor(Math.random() * myArray.length);
return myArray[myIndex];
}
// Set random values to the preferences
sp.measurementUnit = getRandomItem(scriptUnitArray);
vp.horizontalMeasurementUnits = getRandomItem(unitArray);
vp.verticalMeasurementUnits = getRandomItem(unitArray);
vp.strokeMeasurementUnits = getRandomItem(strokeUnitArray);
vp.textSizeMeasurementUnits = getRandomItem(textUnitArray);
vp.typographicMeasurementUnits = getRandomItem(typoUnitArray);
vp.rulerOrigin = getRandomItem(rulerOriginArray);
showCurrentSettings();
}
function resetSettings() {
var defaultUnits = MeasurementUnits.MILLIMETERS;
sp.measurementUnit = AutoEnum.AUTO_VALUE;
vp.horizontalMeasurementUnits = defaultUnits;
vp.strokeMeasurementUnits = defaultUnits;
vp.verticalMeasurementUnits = defaultUnits;
vp.rulerOrigin = RulerOrigin.SPINE_ORIGIN;
vp.textSizeMeasurementUnits = MeasurementUnits.Q;
vp.typographicMeasurementUnits = MeasurementUnits.HA;
showCurrentSettings();
}From pixels to points to ciceros, InDesign allows users to pick from 15 different measurement units to use in various places of their documents. Thanks to this versatility, it can sometimes be hard to predict how a script will act in a measurement unit environment even slightly different from yours.
There are three major places where measurement units could vary depending on the user environment:
Here you will find five important measurementUnit settings that may totally mess up your script if not accounted for.
The measurementUnit settings are:
horizontalMeasurementUnitsverticalMeasurementUnitsstrokeMeasurementUnitstextSizeMeasurementUnitstypographicMeasurementUnitsAlthough some documentation lists all 15 MeasurementUnit enums as possible values for these properties, the truth is that the stroke, textSize, and typographic properties only accept values from a specific subset of these enums. They are:
MeasurementUnits.POINTSMeasurementUnits.MILLIMETERSMeasurementUnits.PIXELSMeasurementUnits.POINTSMeasurementUnits.PIXELSMeasurementUnits.QMeasurementUnits.AMERICAN_POINTSMeasurementUnits.POINTSMeasurementUnits.PIXELSMeasurementUnits.HAMeasurementUnits.AMERICAN_POINTSMeasurementUnits.UMeasurementUnits.BAIMeasurementUnits.MILSSame as document.viewPreferences, but like a default setting for new documents. The document.viewPreferences for each individual document will take precedence over these.
This single property here affects some — but confusingly, not all — ways in which scripts handle units. In addition to all 15 MeasurementUnit enums, there is also a special context-dependent AutoEnum.AUTO_VALUE that uses whatever MeasurementUnit enum is assigned to a relevant property in the viewPreferences. AutoEnum.AUTO_VALUE is the default setting.
I consider this, the scriptPreferences.measurementUnit, to be the most dangerous measurementUnit pitfall. Why? Unlike the viewPreferences, there is no way a casual user of your script will be able to find or change this setting through the standard InDesign GUI. Thus, a reckless script that changes this setting and fails to revert it may alter the behavior of all other scripts in that user’s environment.
There are two things you should do to prevent this disaster. One is to be extremely serious about reverting any changes you make to this property. It’s a good idea to call the reverting routine in the finally of a try clause, so that an unexpected error will not abort the script without reverting.
The second is to script defensively, and make sure your script works regardless of the user’s scriptPreferences.measurementUnit. Do not assume it will always be set to AutoEnum.AUTO_VALUE. If the setting is going to affect any functionality in your script, alter it temporarily to something that is guaranteed to work as intended, and then revert to the original setting.
Check to see how your script will work in random measurement unit environments.
iCal is a personal calendar application made by Apple Inc. that runs on the Mac OS X operating system. iCal was the first calendar application to offer support for multiple calendars and the ability to publish/subscribe calendars to WebDAV server.
For all supported products, you can program your keyboard and/or mouse buttons to perform different functions and to take full advantage of your Logitech keyboard, mouse or trackball.
Included with Mac OS X, Messages (formerly iChat) is a rich instant messaging application that works with your iMessage, Google Talk, AIM, Yahoo!, and Jabeer accounts and makes it easy to stay in touch with friends and family using text and video, whether they’re on a Mac or a PC. Send photos, videos, documents, and contacts — even send messages to a group. You can see when your message has been delivered and when someone’s typing a reply. Turn on read receipts, and they’ll see when you’ve r...
The Finder is the default application/program used on the Mac OS and Mac OS X operating systems that is responsible for the overall user-management of files, disks, network volumes and the launching of other applications. As such, the Finder acts like the shell on other operating systems, but using a graphical user interface. It was introduced with the very first Macintosh computer, and also existed as part of GS/OS on the Apple IIGS. It underwent a complete rewrite with Apple's switch to a U...
BBEdit is the leading professional HTML and text editor for the Macintosh. Specifically crafted in response to the needs of Web authors and software developers, this award-winning product provides an abundance of features for editing, searching, and manipulation of text. BBEdit transforms text with high performance. An intelligent interface provides easy access to BBEdit’s best of class features including grep pattern matching, search and replace across multiple files, function navigation ...
Google Chrome is a web browser developed by Google that uses the WebKit layout engine and application framework. It was first released as a beta version for Microsoft Windows on 2 September 2008, and the public stable release was on 11 December 2008. The name is derived from the graphical user interface frame, or "chrome", of web browsers.
AirPort Utility is the simple to use, setup and management utility for the AirPort Express Base Station, the AirPort Extreme Base Station, and Time Capsule.
Dropbox is the easiest way to store, sync, and, share files online. There's no complicated interface to learn. Dropbox works seamlessly with your operating system and automatically makes sure your files are up-to-date. Available for Windows, Mac, and Linux.
Evernote is a software application that allows users to capture information of various types, including text notes, mobile phone snapshots, printed and handwritten text within images, web clips, and digital ink. All data added to Evernote are run through a series of recognizers that make any text within the various note formats searchable even if it is a photo of a word. The application has three user interfaces: Thumbnails showing clip content, List View showing clip titles only but sortabl...