2011-05-24 116 views
0
on run {input} 
    set filepath to POSIX path of input 
    do shell script "touch " & quoted form of filepath & "untitled" 
    return input 
end run 

是我到目前爲止,它的工作原理,但有沒有辦法,然後專注於該文件,然後觸發重命名?我不希望重命名是自動的,只是觸發事件(如在選擇文件時按「返回」)。我不想用任何一種模式的...AppleScript焦點/重命名文件(並點擊任何地方)

快側的問題:有沒有一種方法來設置這個讓我沒有選擇一個文件夾或文件直接,但可以通過這樣做,可以讓說,只要在Finder中點擊文件夾中的空白區域?現在我的中有「files or folders」服務。

==更新的代碼==

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的作品?

+0

你的意思是什麼?你的意思是在Finder中顯示? – Clark 2011-05-25 01:19:05

+0

就像在網頁開發中一樣:重點。就好像你一次點擊一個文件,並將其標記爲藍色,或者如果你用箭頭鍵導航到文件... – 2011-05-25 02:58:23

回答

3

這是一種方法。我想一個你問名稱的模式窗口會更好,但你可以試試這個。請注意,在此代碼中不使用「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 
+0

查看我的更新代碼。無論出於何種原因,我無法弄清楚如何選擇文件。我嘗試了你的'設置文件路徑'的東西,但是每次運行它時都會出現錯誤。我嘗試了下面Lri所說的選擇(而不是Safari),但也沒有運氣。也是一個錯誤,但如果我保持它設置爲safari它的作品。擊鍵返回是完美的!現在,如果您看到我的更新代碼,那麼我該如何選擇該文件? – 2011-05-26 07:48:09

+0

我修正了你的代碼,見上面的編輯部分。 – regulus6633 2011-05-26 15:07:59

+0

耶!謝啦! – 2011-05-26 16:42:57

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沒有。

我不知道爲什麼,但是當它在列視圖中使用時,您只能選擇前窗的目標的全部內容中的項目。同樣的事情在其他視圖中不會發生,所以它看起來像一個錯誤。


修復了在編輯問題的代碼:(那on go最後行只是用於測試)

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) 

+0

我超級密切...對於我的愛我無法弄清楚如何選擇FILE,但我可以選擇Safari應用程序。我想選擇文件,然後按鍵返回。我嘗試了將Finder窗口1的目標設置爲POSIX文件filepath和「untitled」,但每次都收到錯誤:( – 2011-05-26 07:41:53

+1

Finder文件夾和文件沒有POSIX路徑屬性 - 您需要(如<(Finder文件夾>的文本)'的POSIX路徑)'''filepath的引用形式&'untitled''需要被引用形式(filepath&「untitled」 )'。 – user495470 2011-05-26 10:38:03

0

我基礎上,創建一個AppleScript @ regulus6633的一個,但有一些改進。

注:本答案最初公佈爲AskDifferent answer。爲了方便,我在這裏複製/粘貼。


的想法是創建的Automator工作流和使用以下步驟分配它的快捷方式:

  • 打開的Automator並創建一個服務;
  • 將輸入設置爲無輸入,並將應用程序設置爲Finder.app;
  • 拖放將AppleScript工作流程元素拖放到灰色空間上;
  • 將此AppleScript的內容放入文本框中;
  • 用合理的名稱保存工作流程(如新文件);
  • 轉到設置 - >鍵盤 - >快捷方式 - >服務併爲其指定快捷方式。

現在,讓我們展示的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在this GitHub Gist