0
on run {input}
    set filepath to POSIX path of input
    do shell script "touch " & quoted form of filepath & "untitled"
    return input
end run

これまでのところ、それは機能しますが、ファイルに焦点を合わせて名前の変更をトリガーする方法はありますか? 名前の変更を自動にしたくありません。イベントをトリガーするだけです(ファイルを選択しているときに「return」を押すなど)。そして、私はあらゆる種類のモーダルを使用したくありません...

クイックサイドの質問: これを設定して、フォルダーまたはファイルを直接選択する必要がないようにする方法はありますか? 現在、 で「サービスの受け取り」を「files or folders」に選択していFinder.appます。

== 更新されたコード ==

on run {input}
    set filepath to POSIX path of input
    do shell script "touch " & quoted form of filepath & "untitled"
    tell application "Finder"
        activate
        set target of Finder window 1 to POSIX file "/Users/oscargodson/Documents/designs/untitled"
    end tell
    tell application "System Events"
        tell process "Finder"
            keystroke return
        end tell
    end tell
    return input
end run

パスをハードコーディングすると、機能します! しかし、どうすればそれをvar機能させることができますか?

4

3 に答える 3

3

これが1つの方法です。名前を尋ねるモーダルウィンドウの方が良いと思いますが、これを試すことができます。このコードでは「POSIX パス」を使用していないことに注意してください。Applescript は POSIX パスを使用しません。また、括弧で囲まれている {input} は項目のリストです。したがって、あなたはリストのアイテムを操作します。この場合、最初のアイテムを操作します。

set filepath to item 1 of input

tell application "Finder"
    activate
    reveal filepath
end tell

tell application "System Events"
    tell process "Finder"
        keystroke return
    end tell
end tell

編集:更新されたコードでは、ここに作業スクリプトがあります...

on run {input}
    if (class of input) is not list then set input to {input}
    set theFolder to (item 1 of input) as text

    try
        alias theFolder
        tell application "Finder"
            if (class of item theFolder) is not folder then error "input is not a folder."
            activate
            set theFile to make new file at folder theFolder with properties {name:"untitled"}
            reveal theFile
        end tell

        delay 0.2

        tell application "System Events"
            tell process "Finder"
                keystroke return
            end tell
        end tell
    on error theError number errorNumber
        tell me
            activate
            display dialog "There was an error: " & (errorNumber as text) & return & return & theError buttons {"OK"} default button 1 with icon stop
            return
        end tell
    end try
    return input
end run
于 2011-05-25T13:30:44.987 に答える
1
tell application "Finder"
    activate
    reopen -- in case there are no open windows
    set target of Finder window 1 to POSIX file "/Applications/Safari.app"
end tell

revealselect常に新しいウィンドウを開き、開かないでくださいset targetset selection

理由はわかりませんがset selection、列ビューで使用すると、フロント ウィンドウのターゲットの内容全体の項目しか選択できません。他のビューでは同じことが起こらないので、バグのようです。


編集された質問のコードを修正します。

on go(input)
    set p to POSIX path of (input as text)
    set p2 to p & "untitled"
    do shell script "touch " & p2
    tell application "Finder"
        reopen
        activate
        set target of Finder window 1 to POSIX file p2
    end tell
    delay 0.3 -- time to release modifier keys
    tell application "System Events" to keystroke return
end go

tell application "Finder"
    set fold to folder (path to documents folder)
end tell
go(fold)

(それとon go最後の行はテスト用です。)

于 2011-05-26T06:04:41.497 に答える
0

@regulus6633 のものに基づいて AppleScript を作成しましたが、いくつかの改良が加えられています。

注:この回答は、もともとAskDifferent の回答として投稿されたものです。便宜上、ここにコピー/貼り付けしています。


アイデアは、Automator ワークフローを作成し、次の手順を使用してそれにショートカットを割り当てることです。

  • Automatorを開き、 Serviceを作成します。
  • 入力をno inputに設定し、アプリケーションをFinder.appに設定します。
  • Run AppleScriptワークフロー要素を灰色のスペースにドラッグ アンド ドロップします。
  • この AppleScript の内容をテキストボックスに入れます。
  • ワークフローを適切な名前 ( New Fileなど) で保存します。
  • [設定] -> [キーボード] -> [ショートカット] -> [サービス]に移動し、それにショートカットを割り当てます。

それでは、AppleScript を示しましょう。

set file_name to "untitled"
set file_ext to ".txt"
set is_desktop to false

-- get folder path and if we are in desktop (no folder opened)
try
    tell application "Finder"
        set this_folder to (folder of the front Finder window) as alias
    end tell
on error
    -- no open folder windows
    set this_folder to path to desktop folder as alias
    set is_desktop to true
end try

-- get the new file name (do not override an already existing file)
tell application "System Events"
    set file_list to get the name of every disk item of this_folder
end tell
set new_file to file_name & file_ext
set x to 1
repeat
    if new_file is in file_list then
        set new_file to file_name & " " & x & file_ext
        set x to x + 1
    else
        exit repeat
    end if
end repeat

-- create and select the new file
tell application "Finder"

    activate
    set the_file to make new file at folder this_folder with properties {name:new_file}
    if is_desktop is false then
        reveal the_file
    else
        select window of desktop
        set selection to the_file
        delay 0.1
    end if
end tell

-- press enter (rename)
tell application "System Events"
    tell process "Finder"
        keystroke return
    end tell
end tell

便宜上、この AppleScript をこの GitHub Gistに入れています。

于 2015-04-30T16:29:38.397 に答える