2016-08-23 6 views
1

半複雑なタスクを実行するスクリプトまたは一連のスクリプトを作成しようとしています。私はすでにクロムを自動的に開き、サイトにアクセスし、ログイン情報を入力し、.pkgファイルをダウンロードするという最初のステップを完了しました。私が探しているのは、スクリプトの次の部分を作成するのに役立ちます。これは、ダウンロードフォルダから.pkgファイルを開き、自律的にインストールすることです。ファイル名はQuickAdd.pkgですが、QuickAdd(番号).pkgという名前に変更されています。したがって、最近追加されたフォルダを並べ替える必要があります。以下は、正しいファイルパスを得るために私が今までに考案したスクリプトですが、それは私にとって非常に複雑です。私は自分でこれを完了するのに十分な経験がないので、どんな助けも大いに評価されます。最新のファイルを追加してフォルダをソートするスクリプト、パス名を見つけて.pkgをインストールする

スクリプトV1は:それはある "ファイル:///ユーザ/ wesleycharlap /ダウンロード/(filename.pkg)" などのファイルパスを返す以外


set sourceFolder to (path to downloads folder) 

tell application "Finder" 

set fileList to (sort (get files of sourceFolder) by creation date) 

-- This raises an error if the folder doesn't contain any files 
-- Last File is the Most Recently Created -- 
set theFile to (last item of fileList) as alias 

set thePath to POSIX path of theFile 
set oProp to (properties of theFile) 
set URLstr to URL of oProp 
end tell 

return URLstr 

これは素晴らしい作品、私は、ファイルパスを.pkgとして認識してインストールするようなコマンドはないようです。だから私は問題が "file:///"プレフィックスであると仮定し、したがって私の2番目のスクリプトです。

スクリプトV2:


use framework "Foundation" 
use scripting additions 
set thePath to POSIX path of (path to downloads folder as text) 
set sortedList to its filesIn:thePath sortedBy: 
    (current application's NSURLAddedToDirectoryDateKey) 
    on filesIn:folderPOSIXPath sortedBy:sortKey 
set keysToRequest to {current application's NSURLPathKey, ¬ 
    current application's NSURLIsPackageKey, ¬ 
    current application's NSURLIsDirectoryKey, ¬ 
    sortKey} 
set theFolderURL to current application's class "NSURL"'s fileURLWithPath:folderPOSIXPath 
set theNSFileManager to current application's NSFileManager's defaultManager() 
set listOfNSURLs to (theNSFileManager's contentsOfDirectoryAtURL:theFolderURL ¬ 
    includingPropertiesForKeys:keysToRequest ¬ 
    options:(current application's NSDirectoryEnumerationSkipsHiddenFiles) ¬ 
    |error|:(missing value)) 
set valuesNSArray to current application's NSMutableArray's array() 
repeat with oneNSURL in listOfNSURLs 
    (valuesNSArray's addObject:(oneNSURL's resourceValuesForKeys:keysToRequest |error|:(missing value))) 
end repeat 
set theNSPredicate to current application's NSPredicate's predicateWithFormat_("%K == NO OR %K == YES", current application's NSURLIsDirectoryKey, current application's NSURLIsPackageKey) 
set valuesNSArray to valuesNSArray's filteredArrayUsingPredicate:theNSPredicate 
set theDescriptor to current application's NSSortDescriptor's sortDescriptorWithKey:(sortKey) ascending:true 
set theSortedNSArray to valuesNSArray's sortedArrayUsingDescriptors:{theDescriptor} 
return (theSortedNSArray's lastObject()'s valueForKey:(current application's NSURLPathKey)) as text 
end filesIn:sortedBy: 

このバージョンは確かに私に正しいファイルパス "/Users/wesleycharlap/Downloads/(filename).pkg" を与えるんが、私はどこに混乱していますここから行く

+0

最初のスクリプトで、 "return thePath"で "return URLStr"を変更すると、そのURLではなくパスが得られます: "Users/wesleycharlap/Downloads /(filename.pkg)"。同じスクリプトで、2行目を追加します。「if(count of fileList)= 0 then return」は、フォルダが空の場合にエラーを回避します。 – pbell

+0

応答のためにpbellありがとう、私は愚かな間違いを感じた。 :) –

答えて

0

スクリプトV1を読み取るための

Thxを、それが唯一の.pkgファイルを考慮して、最新のものを開く

set sourceFolder to (path to downloads folder) 

tell application "Finder" 
    set allFiles to files of sourceFolder whose name extension is "pkg" 
    if (count allFiles) = 0 then return 
    set latestFile to last item of (sort allFiles by creation date) 
    open latestFile 
end tell 

に置き換えることができます。 Installer.appはスクリプト可能ではないため、パッケージのインストールプロセスを自動化する次のステップは簡単です。

+0

自動化された.pkgインストールも可能ですか?私は試してみたい!ビデオゲームに見られるような領域のクリックボックスを持つマクロプログラムのようなものでしょうか? Installer.appはデフォルトの位置にデフォルトのサイズでウィンドウを開きますので、理論的にはマクロプログラムが機能するかもしれません。多くのことを助けるあなたの応答btwをありがとう –

+0

理論的にそれは可能ですが、醜いGUIスクリプト – vadian

関連する問題