ラベル Automator の投稿を表示しています。 すべての投稿を表示
ラベル Automator の投稿を表示しています。 すべての投稿を表示

2011年8月20日土曜日

Quartz Composer / Automator / AutomatorのパッチでQCRender関連のナンカを実装してみた

動機
これをやったのでこれ的なQCRenderネタをやってみようかと安易に思った('Quartz コンポジションフィルタをイメージに適用'ってパッチが既にありますが).


やってみた
0.プロジェクトの作成
プロジェクトの修正等はこちらを参照下さい
xibのFile's Ownerのクラスも上記と同じクラス名で修正する(こちらの3.xibを編集を参照のこと).
OpenGLとQuartzのframeworkを追加した.


1.コード

#import <Automator/AMBundleAction.h>
#import <Quartz/Quartz.h>

@interface QuartzComposer : AMBundleAction

- (id)runWithInput:(id)input fromAction:(AMAction *)anAction error:(NSDictionary **)errorInfo;

- (IBAction)openQTZ:(id)sender;

@end



#import "QuartzComposer.h"

@implementation QuartzComposer
- (IBAction)openQTZ:(id)sender
{
NSString *compositionPath= [[NSBundle bundleForClass:[self class]] pathForResource:@"composition" ofType:@"qtz"];
[[NSWorkspace sharedWorkspace] openFile:compositionPath withApplication:@"Quartz Composer"];
}

- (id)runWithInput:(id)input fromAction:(AMAction *)anAction error:(NSDictionary **)errorInfo
{
NSUInteger width= 16;
NSUInteger height= 16;
NSString *compositionPath= [[NSBundle bundleForClass:[self class]] pathForResource:@"composition" ofType:@"qtz"];

QCRenderer *renderer= nil;
{
NSOpenGLPixelFormatAttribute attributes[]= {
NSOpenGLPFAPixelBuffer,
NSOpenGLPFANoRecovery,
NSOpenGLPFAAccelerated,
NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute)24,
NSOpenGLPFASampleBuffers, (NSOpenGLPixelFormatAttribute)1,
NSOpenGLPFASamples, (NSOpenGLPixelFormatAttribute)16,
(NSOpenGLPixelFormatAttribute)0
};
NSOpenGLPixelFormat *format= [[[NSOpenGLPixelFormat alloc] initWithAttributes:attributes] autorelease];
NSOpenGLPixelBuffer *pixelBuffer= [[NSOpenGLPixelBuffer alloc] initWithTextureTarget:GL_TEXTURE_RECTANGLE_EXT
textureInternalFormat:GL_RGBA
textureMaxMipMapLevel:0
pixelsWide:width
pixelsHigh:height];

NSOpenGLContext *openGLContext= [[NSOpenGLContext alloc] initWithFormat:format shareContext:nil];
if(pixelBuffer == nil || openGLContext == nil) {
NSLog(@"no buffer or no context");
return nil;
}

[openGLContext setPixelBuffer:pixelBuffer
cubeMapFace:0
mipMapLevel:0
currentVirtualScreen:[openGLContext currentVirtualScreen]];

renderer= [[QCRenderer alloc] initWithOpenGLContext:openGLContext pixelFormat:format file:compositionPath];
}

NSMutableArray *outputPaths= [NSMutableArray array];
if(renderer) {
NSMutableArray *inputPaths= [NSMutableArray array];
if ([input isKindOfClass:[NSArray class]]) {
[inputPaths addObjectsFromArray:input];
}
else if ([input isKindOfClass:[NSString class]]) {
[inputPaths addObject:input];
}
int i;
for(i=0;i<[inputPaths count];i++) {
NSString *inputPath= [inputPaths objectAtIndex:i];
NSImage *inputImage= [[[NSImage alloc] initWithContentsOfFile:inputPath] autorelease];
[renderer setValue:inputImage forInputKey:@"InputImage"];

if(![renderer renderAtTime:0.0 arguments:nil]) {
NSLog(@"no rendering");
return nil;
}
[renderer createSnapshotImageOfType:@"NSBitmapImageRep"];

NSImage *image= (NSImage *)[renderer valueForOutputKey:@"OutputImage"];
NSBitmapImageRep *bitmapImage= [NSBitmapImageRep imageRepWithData:[image TIFFRepresentation]];
if(bitmapImage) {
char *tempNameBytes = tempnam([NSTemporaryDirectory() fileSystemRepresentation], "QCComposition_Result_");
NSString *tempName = [[[NSString alloc] initWithBytesNoCopy:tempNameBytes
length:strlen(tempNameBytes)
encoding:NSUTF8StringEncoding
freeWhenDone:YES] autorelease];
NSString *outputPath= [tempName stringByAppendingPathExtension:@"png"];


NSDictionary *properties= [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat:1.0], NSImageCompressionFactor,
nil];
NSData *pngImageData= [bitmapImage representationUsingType:NSPNGFileType
properties:properties];

[pngImageData writeToFile:outputPath
atomically:YES];
[outputPaths addObject:outputPath];
}
}
}
else {
NSLog(@"no render");
}

return outputPaths;
}

@end


2.composition.qtzを作成する
Quartz Composerで作成する.この際にInputImageというPublished InputとOutputImageというPublished Outputを用意する.


3.Supporting Files(リソース)にコンポジション(composition.qtz)を加える
Build PhasesのCopy Bundle Resourcesに含まれていること.


4.Viewまわり
xlbにボタンを配置して(IBAction)openQTZ:に繋ぐ.


あとはBuildして配置(~/Library/Automator/下).



結果


Motion Blurなのでこんな感じ.

2011年8月13日土曜日

Xcode4.1のAutomator Plugin ProjectをCocoaでつくる際のテンプレートの修正点

Automator Plugin Projectを作成


Cocoa の指定を確認


プロジェクトの設定(Infoタブ)を確認

Principal classがAMShellScriptActionになぜかなってる.
このまま作成し配置. AMで利用すると実行時に"launch path not accessible"って言われて終了しちゃう.


修正

プロジェクト作成でできるObjective-C class("- (id)runWithInput:(id)input fromAction:(AMAction *)anAction error:(NSDictionary **)errorInfo"の実装を書くオブジェクト.AMBundleActionの派生)名(テンプレートではファイル名と同じ)に修正.

2010年11月26日金曜日

Texの数式文字列を選択して,コンテクストメニューで画像を'Growlで'表示させる

動機:
前回前々回の続き.
方針としては,画像を'/tmp'にdownloadしてしまい,Growlのアイコンとして利用する.
'/tmp'は再起動時に削除されるので放置…。

参考:
  • 「Growl - AppleScript Support for Growl」

  • やってみた:
    0.コード(Automator)


    'シェルスクリプトを実行'0のコード
    use URI::Escape;
    $string = $ARGV[0];
    $string =~ s/^\s*//;
    $string =~ s/[\s\r\n]*$//;
    print "http://chart.apis.google.com/chart?chf=bg,s,00000000&chs=80&cht=tx&chl=" . uri_escape($string) . " \n\"" . $ARGV[0] . "\"";

    'シェルスクリプトを実行'1のコード
    curl $1 > /tmp/math_tex_formulas_to_png.$$.png
    echo "file:///tmp/math_tex_formulas_to_png.$$.png"
    echo $2

    'AppleScriptを実行'のコード
    on run {input, parameters}
    set theFormula to item 2 of input
    display dialog theFormula
    tell application "GrowlHelperApp"
    set the allNotificationsList to {"Image Notification"}
    set the enabledNotificationsList to allNotificationsList
    register as application "Mathematical (Tex) Formulas To PNG" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "Automator"
    notify with name "Image Notification" title "Mathematical (Tex) Formulas To PNG" description theFormula application name "Mathematical (Tex) Formulas To PNG" sticky yes image from location item 1 of input
    end tell
    return input
    end run

    1.Growlの設定
    1.0 上記を一度利用するとGrowlにアプリケーションとして登録される.
    1.1 システム環境設定で確認すると登録されている.

    1.2 デフォルトのスタイルとして前回作成のモノ'MyExample'を指定する.

    2.再度実行

    2010年11月25日木曜日

    2010年11月23日火曜日

    ClipboardへSafariの最前面WindowのTitleとURL,選択文字列をコピーするApplescript

    参考[ AppleScript ] Safari でページタイトルと URL をクリップボードにコピーするサンプル | Bowz::Notebook 感謝.

    tell application "Safari"
    set ThisText to do JavaScript "window.getSelection()" in front document
    set ThisURL to do JavaScript "document.URL" in front document
    set ThisTitle to do JavaScript "document.title" in front document
    if ThisText is equal to "" then
    set the clipboard to "「" & ThisTitle & "」 " & ThisURL
    else
    set the clipboard to "“" & ThisText & "” 「" & ThisTitle & "」 " & ThisURL
    end if
    end tell

    っていうか 'do JavaScript "…"'なんてできるんだ.


    で,Automatorでサービスにしてみる.



    追記(2010/11/25): 入力をテキストにすると選択文字列があるときだけ利用できる.選択文字列があるときにコンテクストメニューに項目がでる.



    2008年3月23日日曜日

    AutomatorからTwitterへPostする(中身はcurl)

    追記(2011/8/13): OAuthConsumer.frameworkを用いてOAuth対応Cocoa版を作ってみました


    まぁやり方は参考のままApplescriptでcurlをタタイているのですが一応外側としてAutomatorにしてみました.
    http://y30.net/pg/plugins/Automator/
    y30.net/pg/は閉鎖



    名前
    PostInTwitter
    機能
    TwitterへPostします.
    スクリーンショット

    ダウンロード
    PostInTwitter.action.zip
    インストール
    zipを展開して'PostInTwitter.action'をダブルクリックしてインストール.
    使い方
    username, passwordにTwitterのアカウント情報を,入力にPOSTする文字列を用意してください.例えば'テキスト/テキストの入力を求める'などを前に設置してあげてください.
    注意事項
    ワークフローを保存すると内部に平文でpasswordが記録されます.
    参考
    http://www.leroux.ca/archive/dsp_view_474.cfm
    http://www.openspc2.org/reibun/Automator/
    免責
    "AS IS"でお願いします.