
[英]How to pass several variables from Shell Script to AppleScript in Automator?
[英]How do I rename and move a file downloaded to 'Downloads" using Automator, with or without Applescript, Shell Script or Javascript?
我是編程新手,因此請原諒我缺乏知識。 我已經搜索了網站和互聯網,但沒有找到看似簡單問題的答案。
我想自動化一些個人和商業文件的歸檔和重命名——它們是銀行對賬單,所以數字是匿名的。 我對理解代碼很感興趣,因此我也可以在之后對其進行調整,以進行進一步的操作(也許供其他人使用)。
文檔將下載到 (mac) 下載文件夾中。 通常他們有這個名字:“Statement--12345678--98765432--1-06-2020-30-06-2020.pdf”開頭的兩組數字不是這些通用數字而是8個數字(雖然第一個數字有時沒有列出,因為它是“0”)。 第二組兩個數字是指兩個日期,采用日-月-年格式。 有時第一個日期從上個月的最后一天開始!
作為一個新手,我從 Automator 開始 - 使用文件夾操作將單個文件移動到命名文件夾(按年份)。 然后我想重命名它們,以便第二個日期在 YYYYMMDD 格式的名稱中排在第一位,這樣它們就會自動按日期順序列在年份文件夾中。 全名將變為“YYYYMMDD 98765432 Month YY”。
我可以自動移動文件(感謝automator); 我什至可以以正確的格式在名稱的開頭添加當前日期(但它將是當前日期而不是文件中的日期)。 但我不能做我真正想做的事:根據文件名中的日期更改名稱。
然后我查看了 AppleScript。 下面的答案解決了命名問題 - 謝謝!
但是當我嘗試拿起一堆文件時 - 其中有 25 個(很高興被 Automator 找到並移動(查找文件和移動文件)output 未被識別為 AppleScript 的輸入。我得到“無法獲取文件XXXX 作為別名”,或者如果我嘗試創建一個未定義的變量(盡管我已經嘗試了很多次......作為 {},作為“”,作為輸入的第 1 項)。
如果不清楚,我深表歉意,但我正在盡力解釋它,並且不理解諸如“終端 ls”之類的術語。
感謝您收到任何幫助、建議和評論。 我真的很想嘗試理解代碼,這樣我就可以應用所學,謝謝你,約翰
好的,您的問題是提取名稱的多個部分。 訣竅是把它炸成小塊。 GREP 是一個很好的工具,但對於 applescript “開箱即用”來說很棘手。
我使用一個名為“textSplit”的子程序來完成這項工作。 一旦文件名的每個部分都在變量中可用,您應該能夠構建任何文件或文件夾名稱......
這是我解決這個問題的方法:
set thisFileName to "Document--12345678--98765432--1-06-2020-30-06-2020.pdf"
-- first we split the name using the "--" separator
set mainParts to textSplit(thisFileName, "--")
-- we now copy the result to variables to handle it
copy mainParts to {prefixOne, numberOne, numberTwo, theTwoDates}
--> prefixOne = "Document"
--> numberOne = "12345678"
--> numberTwo = "98765432"
--> theTwoDates = "1-06-2020-30-06-2020.pdf"
-- get rid of the extension
set theDatesWithoutExtension to first item of textSplit(theTwoDates, ".")
-- split the dates
set splitDates to textSplit(theDatesWithoutExtension, "-")
-- copy result into nice variables
copy splitDates to {dayOne, monthOne, yearOne, dayTwo, monthTwo, yearTwo}
-- and then build the filename with whatever you want
set myNewFileName to yearOne & monthOne & dayOne & space & numberTwo & space & monthTwo & "-" & yearTwo & ".pdf"
--> "2020061 98765432 06-2020.pdf"
-- make folders, move files, rename…
-- ================================================================================================
on textSplit(theText, theDelimiter)
-- this will split a string and returns a list of substrings
set saveDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to {theDelimiter}
set theList to every text item of theText
set AppleScript's text item delimiters to saveDelim
return (theList)
end textSplit
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.