天天看點

AI小技能:讓ChatGPT幫助整理Mac上的應用

作者:愉悅的光束xAM

蘋果筆記本上裝了一大堆各種應用,很多應用比較少用,使用spotlight進行查找時,又因為不記得應用的名字,隻好在一大堆圖示中仔細辨識;

一個比較好的方法就是給應用程式加上備注,這樣能通過spotlight查找到,但是這種繁瑣的事情怎麼不讓AI來幫忙,是以開始呼喚ChatGPT了。

第一步:讓ChatGPT給我寫個腳本,幫忙把已經安裝的應用名字全導出到一個檔案

Prompt提示詞這麼寫:

請用AppleScript編寫一個腳本,這個腳本的功能是從我的Application目錄下把所有已經
安裝的應用,讀取應用的路徑,名稱和備注屬性,按照“路徑|名稱|備注”的格式生成一個文本檔案           

ChatGPT很快就給出了一個AppleScript的代碼例子,自己再人工稍微調整了一下:

-- 指定輸出檔案的路徑
set outputPath to (path to desktop as text) & "applications.txt"

-- 擷取 "/Applications" 檔案夾中的所有應用程式
set appFolder to POSIX path of (path to applications folder)
set appList to list folder appFolder without invisibles
-- 打開輸出檔案以寫入應用程式資訊
set outputFile to open for access outputPath with write permission
-- 周遊應用程式并提取名稱和 "Comments" 屬性
repeat with appName in appList
	set appPath to (appFolder & appName) as text
	-- 使用 mdls 指令檢查是否是應用程式
	set isApplication to (do shell script "[ -d " & quoted form of appPath & " ] && mdls -name kMDItemContentTypeTree -raw " & quoted form of appPath & " | grep -q 'com.apple.application' && echo 'true' || echo 'false'") is "true"
	-- 擷取應用程式名稱
	-- set appNameText to name of (info for appPath)
	-- 擷取應用程式 "Comments" 屬性
	if isApplication then
		try
			set appNameText to (do shell script "mdls -name kMDItemDisplayName -raw -nullMarker '' -nokids -nobanner -nolocal " & quoted form of appPath)
		on error
			set appNameText to ""
		end try
		-- 擷取應用程式 "Comments" 屬性
		try
			set comments to (do shell script "mdls -name kMDItemFinderComment -raw -nullMarker '' -nokids -nobanner -nolocal " & quoted form of appPath)
		on error
			set comments to ""
		end try
		-- 将應用程式名稱和 "Comments" 寫入輸出檔案
		write (appPath & "|" & appNameText & "|" & comments & linefeed) to outputFile as «class utf8»
	end if
end repeat

-- 關閉輸出檔案
close access outputFile
-- 顯示完成消息
display dialog "應用程式資訊已寫入 " & outputPath           

将代碼複制到系統自帶的腳本編輯器(Script Editor),運作後就能在桌面生成一個applications.txt檔案,裡面已經存放了所有在application目錄下安裝的應用資訊;

application.txt部分内容:

/Applications/Spark.app|Spark|
/Applications/Postgres.app|Postgres|
/Applications/HelloFont.app|HelloFont|
/Applications/VOX.app|VOX|
/Applications/Serviio-Console.app|Serviio-Console|
/Applications/SourceTree.app|SourceTree|
/Applications/LimeChat.app|LimeChat|
/Applications/Text Scanner.app|Text Scanner|
/Applications/ProtoPie.app|ProtoPie|
/Applications/CapCut.app|CapCut|
/Applications/Altair GraphQL Client.app|Altair GraphQL Client|
/Applications/百度翻譯.app|百度翻譯|
...           

第二步:讓ChatGPT幫忙整理出每個應用的簡介

這一步讓ChatGPT自動根據提供的應用名稱來總結應用的簡介和分類;

提示詞:

接下來上傳的内容,每一行的格式為“路徑|應用|備注”,請用一句話生成應用的分類描述,
并寫入每行的備注字段,以excel格式輸出           

然後把第一步得到的application.txt内容貼到對話,ChatGPT很快就給出了分析後的内容

AI小技能:讓ChatGPT幫助整理Mac上的應用

讓chatgpt幫助整理應用的備注

這裡可以根據自己的需要調整提示詞(Prompt) ,讓他給出你喜歡的描述方式

将生成的内容複制到檔案application_update.txt儲存;

第三步:讓ChatGPT再寫個腳本,把應用的簡介寫到每個應用到備注裡

這一步和第一步一樣,讓chatGPT來幫助寫腳本,把更新後的application_update.txt内容分别寫到每個應用的備注項中。

請使用applescript開發一個腳本,這個腳本功能是從"application_update.txt"檔案中讀取内容,内容格式為“apppath|app|comment”的清單,
腳本需要依次讀取apppath和comment,根據apppath指定的應用,更新這個應用的kMDItemFinderComment屬性為comment的内容           

同樣,chatgpt給出的腳本,經過我調整一下後,直接貼到腳本編輯器(Script Editor)中運作

-- 請将以下路徑替換為你的文本檔案路徑
set filePath to (path to desktop as text) & "applications_updated.txt"

try
	-- 打開文本檔案并按行讀取内容
	set fileContents to read file filePath as «class utf8»
	
	-- 将文本檔案内容分割成行
	set textItems to paragraphs of fileContents
	
	-- 循環周遊每行内容
	repeat with lineText in textItems
		try
			-- 使用管道符分割應用路徑和注釋
			set AppleScript's text item delimiters to "|"
			set lineItems to text items of lineText
			set appPath to item 1 of lineItems
			set commentText to item 2 of lineItems
			
			-- 建立 AppleScript 代碼來設定應用程式的 kMDItemFinderComment 屬性
			set pathCommentScript to "set filepath to (POSIX file \"" & appPath & "\" as alias)"
			set setCommentScript to "tell application \"Finder\" to set comment of filepath to \"" & commentText & "\""
			
			-- 運作 AppleScript 代碼
			do shell script "osascript -e " & quoted form of pathCommentScript & " -e " & quoted form of setCommentScript
			
			-- 使用 xattr 指令來設定應用程式的注釋屬性
			--do shell script "xattr -w com.apple.metadata:kMDItemFinderComment " & quoted form of commentText & " " & quoted form of appPath with administrator privileges

		on error errMsg
			display dialog "設定注釋屬性失敗: " & errMsg
		end try
		
	end repeat
on error errMsg
	display dialog "無法讀取檔案: " & errMsg
end try           

最終結果:在finder中顯示應用的備注,并且可以在spotlight中按照你的備注詞搜尋需要的應用

在檔案中就可以直接把comment字段顯示出來,友善檢視;

AI小技能:讓ChatGPT幫助整理Mac上的應用

利用chatgpt生成備注的應用顯示效果

在spotlight快捷搜尋時,可以直接輸入關鍵詞 kind:app來快速找到這類應用

AI小技能:讓ChatGPT幫助整理Mac上的應用

通過關鍵詞快捷查找

繼續閱讀