NSURLBookmark

エイリアス・シンボリックリンクの参照元の取得 基本型


AppleScript サンプルコード

【スクリプトエディタで開く】 |

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004#com.cocolog-nifty.quicktimer.icefloe
005----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
006use AppleScript version "2.8"
007use framework "Foundation"
008use framework "UniformTypeIdentifiers"
009use framework "AppKit"
010use scripting additions
011property refMe : a reference to current application
012
013
014#シンボリックリンク
015set strFilePath to ("/Library/Fonts/Arial Unicode.ttf") as text
016
017#パスURL
018set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
019set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
020set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
021
022#エイリアスか?確認
023set listResponse to (ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsAliasFileKey) |error| :(reference))
024set boolIs to (item 2 of listResponse) as boolean
025if boolIs is true then
026  #シンボリックリンクか?確認
027  set listResponse to (ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsSymbolicLinkKey) |error| :(reference))
028  set boolIs to (item 2 of listResponse) as boolean
029  if boolIs is true then
030    log "エイリアスではありません シンボリックリンクです"
031    set ocidResolvedURL to ocidFilePathURL's URLByResolvingSymlinksInPath()
032    if ocidResolvedURL = (missing value) then
033      display alert "参照先がすでに削除済みです"
034    else
035      set strResolvedPath to ocidResolvedURL's |path|() as text
036    end if
037  else
038    log "エイリアスです"
039    #URLByResolvingAliasFileAtURLで取得を試みる
040    (*
041NSURLBookmarkResolutionOptions
042NSURLBookmarkResolutionWithoutUI:8
043NSURLBookmarkResolutionWithoutMounting:9
044NSURLBookmarkResolutionWithSecurityScope:10
045NSURLBookmarkResolutionWithoutImplicitStartAccessing:15
046*)
047    set option to (42) as integer
048    set listResponse to refMe's NSURL's URLByResolvingAliasFileAtURL:(ocidFilePathURL) options:(option) |error| :(reference)
049    set ocidResolvedURL to (item 1 of listResponse)
050    #取得できなければ
051    if ocidResolvedURL = (missing value) then
052      #bookmarkData を使って取得を試みる
053      set listResponse to refMe's NSURL's bookmarkDataWithContentsOfURL:(ocidFilePathURL) |error| :(reference)
054      set ocdiBookMarkData to (item 1 of listResponse)
055      set listResponse to refMe's NSURL's URLByResolvingBookmarkData:(ocdiBookMarkData) options:(refMe's NSURLBookmarkResolutionWithoutUI) relativeToURL:(missing value) bookmarkDataIsStale:("isStale") |error| :(reference)
056      set ocidResolvedURL to (item 1 of listResponse)
057      display alert "参照先がすでに削除済みです"
058    else
059      set strResolvedPath to ocidResolvedURL's |path|() as text
060    end if
061  end if
062else
063  return "エイリアスではありません"
064end if
065
066
067#
AppleScriptで生成しました

|

エイリアス・シンボリックリンクの参照元の取得


AppleScript サンプルコード

【スクリプトエディタで開く】 |

AppleScript サンプルソース(参考)
行番号ソース
001#!/usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004#com.cocolog-nifty.quicktimer.icefloe
005----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
006use AppleScript version "2.8"
007use framework "Foundation"
008use framework "UniformTypeIdentifiers"
009use framework "AppKit"
010use scripting additions
011property refMe : a reference to current application
012
013#############################
014#前面に
015set strName to (name of current application) as text
016if strName is "osascript" then
017  tell application "Finder" to activate
018else
019  tell current application to activate
020end if
021#デスクトップ
022set appFileManager to refMe's NSFileManager's defaultManager()
023set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
024set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
025set aliasDesktopDirPath to (ocidDesktopDirPathURL's absoluteURL()) as alias
026#
027set listUTI to {"com.apple.resolvable", "com.apple.alias-file", "public.symlink"} as list
028set strMes to ("エイリアスファイルを選んでください") as text
029set strPrompt to ("エイリアスファイルを選んでください") as text
030try
031  set aliasFilePath to (choose file strMes with prompt strPrompt default location (aliasDesktopDirPath) of type listUTI with invisibles and showing package contents without multiple selections allowed)
032on error
033  log "エラーしました"
034  return "エラーしました"
035end try
036
037set strFilePath to (POSIX path of aliasFilePath) as text
038#パスURL
039set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
040set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
041set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
042#エイリアスか?確認
043set listResponse to (ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsAliasFileKey) |error| :(reference))
044set boolIs to (item 2 of listResponse) as boolean
045if boolIs is true then
046  #シンボリックリンクか?確認
047  set listResponse to (ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsSymbolicLinkKey) |error| :(reference))
048  set boolIs to (item 2 of listResponse) as boolean
049  if boolIs is true then
050    log "エイリアスではありません シンボリックリンクです"
051    set ocidResolvedURL to ocidFilePathURL's URLByResolvingSymlinksInPath()
052    if ocidResolvedURL = (missing value) then
053      display alert "参照先がすでに削除済みです"
054    else
055      set strResolvedPath to ocidResolvedURL's |path|() as text
056    end if
057  else
058    log "エイリアスです"
059    set listResponse to refMe's NSURL's bookmarkDataWithContentsOfURL:(ocidFilePathURL) |error| :(reference)
060    set ocdiBookMarkData to (item 1 of listResponse)
061    set listResponse to refMe's NSURL's URLByResolvingBookmarkData:(ocdiBookMarkData) options:(refMe's NSURLBookmarkResolutionWithoutUI) relativeToURL:(missing value) bookmarkDataIsStale:("isStale") |error| :(reference)
062    set ocidResolvedURL to (item 1 of listResponse)
063    if ocidResolvedURL = (missing value) then
064      display alert "参照先がすでに削除済みです"
065    else
066      set strResolvedPath to ocidResolvedURL's |path|() as text
067    end if
068  end if
069else
070  return "エイリアスではありません"
071end if
072
073#
074set strName to (name of current application) as text
075if strName is "osascript" then
076  tell application "Finder" to activate
077else
078  tell current application to activate
079end if
080set aliasIconPath to (POSIX file "/System/Library/CoreServices/Finder.app/Contents/Resources/Finder.icns") as alias
081try
082  set recordResult to (display dialog strResolvedPath with title "戻り値です" default answer strResolvedPath buttons {"クリップボードにコピー", "終了", "Finderで参照"} default button "Finderで参照" cancel button "終了" giving up after 20 with icon aliasIconPath without hidden answer) as record
083on error
084  return "エラーしました"
085end try
086if (gave up of recordResult) is true then
087  return "時間切れです"
088end if
089##############################
090#Finderで参照
091##############################
092if button returned of recordResult is "Finderで参照" then
093  #コンテナ
094  set ocidContainerDirPathURL to ocidResolvedURL's URLByDeletingLastPathComponent()
095  #Finderで選択
096  set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
097  set boolDone to appSharedWorkspace's selectFile:(ocidResolvedURL's |path|()) inFileViewerRootedAtPath:(ocidContainerDirPathURL's |path|())
098  #エラーしたら
099  if boolDone is false then
100    #親フォルダを選択する
101    set ocidOpenURLsArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:0)
102    (ocidOpenURLsArray's addObject:(ocidResolvedURL))
103    appSharedWorkspace's activateFileViewerSelectingURLs:(ocidContainerDirPathURL)
104  end if
105  return
106end if
107##############################
108#####値のコピー
109##############################
110if button returned of recordResult is "クリップボードにコピー" then
111  try
112    set strText to text returned of recordResult as text
113    ####ペーストボード宣言
114    set appPasteboard to refMe's NSPasteboard's generalPasteboard()
115    set ocidText to (refMe's NSString's stringWithString:(strResolvedPath))
116    appPasteboard's clearContents()
117    appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
118  on error
119    tell application "Finder"
120      set the clipboard to strResolvedPath as text
121    end tell
122  end try
123end if
124return
125
AppleScriptで生成しました

|

NSURLBookmarkCreationOptions


あくまでも参考にしてください

【スクリプトエディタで開く】 |

サンプルソース(参考)
行番号ソース
001#! /usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003(*
004
005どれが正解なんだろう…苦笑
006可能ならセキュアな
007NSURLBookmarkCreationSecurityScopeAllowOnlyReadAccess
008NSURLBookmarkCreationWithSecurityScopeを利用したいが
009エラーになるパスもあるので~/Libraryとかね
010NSURLBookmarkCreationWithoutImplicitSecurityScopeでもいいか
011*)
012----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
013use AppleScript version "2.8"
014use framework "Foundation"
015use scripting additions
016
017property refMe : a reference to current application
018
019log (refMe's NSURLBookmarkCreationPreferFileIDResolution) as integer
020-->(*256*)
021log (refMe's NSURLBookmarkCreationMinimalBookmark) as integer
022-->(*512*)
023log (refMe's NSURLBookmarkCreationSuitableForBookmarkFile) as integer
024-->(*1024*)
025log (refMe's NSURLBookmarkCreationWithSecurityScope) as integer
026-->(*2048*)
027log (refMe's NSURLBookmarkCreationSecurityScopeAllowOnlyReadAccess) as integer
028-->(*4096*)
029log (refMe's NSURLBookmarkCreationWithoutImplicitSecurityScope) as text
030-->(*536870912*)
AppleScriptで生成しました

|

[URLByResolvingBookmarkData]BOOKMARKデータをURLにする


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions
property refMe : a reference to current application

##デスクトップ
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
##BOOKMARKにする
set listDone to (ocidDesktopDirPathURL's bookmarkDataWithOptions:(refMe's NSURLBookmarkCreationSuitableForBookmarkFile) includingResourceValuesForKeys:{refMe's NSURLCustomIconKey} relativeToURL:(missing value) |error|:(reference))
set ocdiBookMarkData to (item 1 of listDone)

##BOOKMARKをエイリアスファイルにする
set ocidSaveAliasFilePathURL to ocidDesktopDirPathURL's URLByAppendingPathComponent:("デスクトップのエイリアス")
#
set listResults to (refMe's NSURL's writeBookmarkData:(ocdiBookMarkData) toURL:(ocidSaveAliasFilePathURL) options:(refMe's NSURLBookmarkCreationSuitableForBookmarkFile) |error|:(reference))


##エイリアスファイルを読み込む
set listResults to refMe's NSURL's bookmarkDataWithContentsOfURL:(ocidSaveAliasFilePathURL) |error|:(reference)
set ocdiBookMarkData to (item 1 of listResults)

##BOOKMARKデータをURLにする
set listResponse to (refMe's NSURL's URLByResolvingBookmarkData:(ocdiBookMarkData) options:(refMe's NSURLBookmarkResolutionWithoutUI) relativeToURL:(missing value) bookmarkDataIsStale:(missing value) |error|:(reference))
set ocidBookMarkURL to (item 1 of listResponse)
log ocidBookMarkURL's absoluteString() as text

##BOOKMARK=エイリアスの参照先URLを取得
set listResponse to (refMe's NSURL's URLByResolvingAliasFileAtURL:(ocidSaveAliasFilePathURL) options:(refMe's NSURLBookmarkResolutionWithoutUI) |error|:(reference))
set ocidBookMarkURL to (item 1 of listResponse)
log ocidBookMarkURL's absoluteString() as text



|

[NSKeyedUnarchiver]com.apple.LSSharedFileListのBookMarkデータを表示(macOS14対応)


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
# com.apple.LSSharedFileListのBookMarkデータを表示します
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions
property refMe : a reference to current application

set appFileManager to refMe's NSFileManager's defaultManager()

###################################
#####ファイル選択ダイアログ
###################################
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder"
activate
  end tell
else
  tell current application
activate
  end tell
end if
####ダイアログを出す
set listUTI to {"com.apple.property-list", "dyn.ah62d4rv4ge81g3xqgq"} as list
set aliasFilePath to (choose file with prompt "plistファイルを選んでください" default location (aliasDefaultLocation) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias
####入力ファイルパス
set strFilePath to POSIX path of aliasFilePath
set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
set strExtensionName to (ocidFilePathURL's pathExtension()) as text

###################################
#####処理分岐
###################################
if strExtensionName is "plist" then
  # DICTに読み込んで
  set listResponse to refMe's NSDictionary's alloc()'s initWithContentsOfURL:(ocidFilePathURL) |error|:(reference)
  set ocidPlistDict to (item 1 of listResponse)
else if strExtensionName is "sfl3" then
  # NSDataに読み込んで
  set ocidReadData to refMe's NSData's dataWithContentsOfURL:(ocidFilePathURL)
  # unarchivedObjectOfClassで解凍する
  set listResponse to refMe's NSKeyedUnarchiver's unarchivedObjectOfClass:((refMe's NSObject)'s class) fromData:(ocidReadData) |error|:(reference)
  set ocidPlistDict to (item 1 of listResponse)
  if ocidPlistDict = (missing value) then
return "解凍に失敗しました"
  end if
end if

###################################
#####
###################################
set ocidItemsArray to (ocidPlistDict's objectForKey:("items"))
repeat with itemsArrayDict in ocidItemsArray
  set ocidBookMarkData to (itemsArrayDict's objectForKey:("Bookmark"))
  #BOOKMarkデータの解凍
  set listResponse to (refMe's NSURL's URLByResolvingBookmarkData:(ocidBookMarkData) options:(refMe's NSURLBookmarkResolutionWithoutUI) relativeToURL:(missing value) bookmarkDataIsStale:(missing value) |error|:(reference))
  set ocidBookMarkURL to (item 1 of listResponse)
log ocidBookMarkURL's absoluteString() as text
  
end repeat



|

デスクトップにiCloudDriveのエイリアスを作る(アイコン付き)

Screen-20231106-150755

【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
# デスクトップにクラウドドライブのエイリアスを作ります
# Finderでは『iCloud Drive』と表示されていますが
# フォルダ名は『Mobile Documents』なので留意
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application

set appFileManager to refMe's NSFileManager's defaultManager()

####################################
###ドコ?に作る?
set strAddAliasFilePath to ("~/Desktop/iCloudDrive") as text

###アイコンどうする?
set boolSetIcon to true as boolean
###アイコン付きの場合
if boolSetIcon is true then
  ###iCloudDrive.icnsのパス
  set strIconFilePath to "/System/Library/PrivateFrameworks/iCloudDriveCore.framework/Versions/A/Resources/iCloudDrive.icns" as text
end if

####################################
####エイリアスが作られる場所
set ocidAddAliasFilePathStr to refMe's NSString's stringWithString:(strAddAliasFilePath)
set ocidAddAliasFilePath to ocidAddAliasFilePathStr's stringByStandardizingPath
set ocidAddAliasFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidAddAliasFilePath) isDirectory:false
set strAddAliasFilePathURL to ocidAddAliasFilePathURL's |path|() as text

###ディレクトリを作る必要があれば作る
set ocidAddAliasDirFilePathURL to ocidAddAliasFilePathURL's URLByDeletingLastPathComponent()
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocidAttrDict's setValue:511 forKey:(refMe's NSFilePosixPermissions)
set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidAddAliasDirFilePathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)

####################################
####エイリアスの元ファイル
####################################
set ocidUserLibraryPathArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidUserLibraryPathURL to ocidUserLibraryPathArray's objectAtIndex:0
set ocidiCloudDriveURL to ocidUserLibraryPathURL's URLByAppendingPathComponent:"Mobile Documents"

####################################
#### エイリアスを作る
####################################
set listDone to (ocidiCloudDriveURL's bookmarkDataWithOptions:(refMe's NSURLBookmarkCreationSuitableForBookmarkFile) includingResourceValuesForKeys:{refMe's NSURLCustomIconKey} relativeToURL:(missing value) |error|:(reference))
set ocdiBookMarkData to (item 1 of listDone)

set listResults to (refMe's NSURL's writeBookmarkData:(ocdiBookMarkData) toURL:(ocidAddAliasFilePathURL) options:(refMe's NSURLBookmarkCreationSuitableForBookmarkFile) |error|:(reference))
log item 1 of listResults
####################################
#### アイコンを付与
####################################
if boolSetIcon is true then
  ###ICONパス
  set ocidIconPathStr to refMe's NSString's stringWithString:(strIconFilePath)
  set ocidIconPath to ocidIconPathStr's stringByStandardizingPath()
  set ocidIconPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidIconPath) isDirectory:false)
  ##アイコン用のイメージデータ取得
  set ocidImageData to (refMe's NSImage's alloc()'s initWithContentsOfURL:(ocidIconPathURL))
  ###NSWorkspace初期化
  set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
  ###アイコン付与
  set boolAddIcon to (appSharedWorkspace's setIcon:(ocidImageData) forFile:(ocidAddAliasFilePath) options:(refMe's NSExclude10_4ElementsIconCreationOption))
end if

return

|

[NSURLBookmarkCreationOptions]8bit値が戻り値


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#com.cocolog-nifty.quicktimer.icefloe
#ランチパッドの縦横行列数の変更
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "Appkit"
use framework "UniformTypeIdentifiers"
use scripting additions
property refMe : a reference to current application


log (refMe's NSURLBookmarkCreationPreferFileIDResolution) as integer (*256*)
log (refMe's NSURLBookmarkCreationMinimalBookmark) as integer (*512*)
log (refMe's NSURLBookmarkCreationSuitableForBookmarkFile) as integer (*1024*)
log (refMe's NSURLBookmarkCreationWithSecurityScope) as integer (*2048*)
log (refMe's NSURLBookmarkCreationSecurityScopeAllowOnlyReadAccess) as integer (*4096*)
log (refMe's NSURLBookmarkCreationWithoutImplicitSecurityScope) as integer (*5.36870912E+8*)


log (refMe's NSURLBookmarkResolutionWithoutUI) as integer (*256*)
log (refMe's NSURLBookmarkResolutionWithoutMounting) as integer (*512*)
log (refMe's NSURLBookmarkResolutionWithSecurityScope) as integer (*1024*)
log (refMe's NSURLBookmarkResolutionWithoutImplicitStartAccessing) as integer (*32768*)



log (refMe's NSURLBookmarkCreationPreferFileIDResolution) / 256 as integer (*1*)
log (refMe's NSURLBookmarkCreationMinimalBookmark) / 256 as integer (*2*)
log (refMe's NSURLBookmarkCreationSuitableForBookmarkFile) / 256 as integer (*4*)
log (refMe's NSURLBookmarkCreationWithSecurityScope) / 256 as integer (*8*)
log (refMe's NSURLBookmarkCreationSecurityScopeAllowOnlyReadAccess) / 256 as integer (*16*)
log (refMe's NSURLBookmarkCreationWithoutImplicitSecurityScope) / 256 as integer (*2097152*)


log (refMe's NSURLBookmarkResolutionWithoutUI) / 256 as integer (*1*)
log (refMe's NSURLBookmarkResolutionWithoutMounting) / 256 as integer (*2*)
log (refMe's NSURLBookmarkResolutionWithSecurityScope) / 256 as integer (*4*)
log (refMe's NSURLBookmarkResolutionWithoutImplicitStartAccessing) / 256 as integer (*128*)






|

アプリケーションのエイリアスをデスクトップに作成する


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "UniformTypeIdentifiers"
use framework "AppKit"
use scripting additions
property refMe : a reference to current application

###初期化
set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
set appFileManager to refMe's NSFileManager's defaultManager()
#####ダイアログを前面に
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
###ダイアログ
set listUTI to {"com.apple.application-bundle"}
###アプリケーションディレクトリ
set ocidUserLibraryPathArray to (appFileManager's URLsForDirectory:(refMe's NSApplicationDirectory) inDomains:(refMe's NSUserDomainMask))
###URL
set ocidAppDirPathURL to ocidUserLibraryPathArray's firstObject()
set aliaAppDirPath to (ocidAppDirPathURL's absoluteURL()) as alias
set aliasAppPath to (choose file with prompt "対象のアプリケーションを選んでください" default location (aliaAppDirPath) of type listUTI with invisibles without showing package contents and multiple selections allowed) as alias
###パス
set strAppPath to POSIX path of aliasAppPath
set ocidAppPathStr to refMe's NSString's stringWithString:(strAppPath)
set ocidAppPath to ocidAppPathStr's stringByStandardizingPath()
set ocidAppPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidAppPath) isDirectory:true)

####アプリのアイコンパス
###バンドル取得
set ocidAppBundle to refMe's NSBundle's bundleWithURL:(ocidAppPathURL)
###情報取得
set ocidInfoDict to ocidAppBundle's infoDictionary
###アプリケーション名
set ocidBundleDisplayName to ocidInfoDict's valueForKey:("CFBundleDisplayName")
###CFBundleDisplayNameが無い場合
if ocidBundleDisplayName = (missing value) then
  log "CFBundleName名を使います"
  set ocidBundleDisplayName to ocidInfoDict's valueForKey:("CFBundleName")
end if
###アイコン名
set strIconFileName to (ocidInfoDict's valueForKey:("CFBundleIconFile")) as text
###ICONのURLにして
set strPath to ("Contents/Resources/" & strIconFileName) as text
set ocidIconFilePathURL to ocidAppPathURL's URLByAppendingPathComponent:(strPath) isDirectory:false
###拡張子の有無チェック
set strExtensionName to (ocidIconFilePathURL's pathExtension()) as text
if strExtensionName is "" then
  set ocidIconFilePathURL to ocidIconFilePathURL's URLByAppendingPathExtension:"icns"
end if
###パス
set ocidIconFilePath to ocidIconFilePathURL's |path|
###ICONファイルが実際にあるか?チェック
set boolExists to appFileManager's fileExistsAtPath:(ocidIconFilePath)
###ICONがみつかない時用にデフォルトを用意する
if boolExists is false then
  set strIconPath to POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/GenericQuestionMarkIcon.icns"
  set ocidIconPathstr to refMe's NSString's stringWithString:(strIconPath)
  set ocidIconPath to ocidIconPathstr's stringByStandardizingPath()
  set ocidIconFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidIconPath) isDirectory:false)
  set aliasIconPath to ocidIconFilePathURL's absoluteURL() as alias
else
  set aliasIconPath to ocidIconFilePathURL's absoluteURL() as alias
end if
###########################
##アイコン用のイメージデータ取得
set ocidImageData to (refMe's NSImage's alloc()'s initWithContentsOfURL:(ocidIconFilePathURL))
###########################
###bookmark エイリアスデータ
set listBookMarkNSData to (ocidAppPathURL's bookmarkDataWithOptions:(refMe's NSURLBookmarkCreationSuitableForBookmarkFile) includingResourceValuesForKeys:{refMe's NSURLCustomIconKey} relativeToURL:(missing value) |error|:(reference))
set ocdiBookMarkData to (item 1 of listBookMarkNSData)

###########################
###デスクトップ
set ocidDesktopPathArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidDesktopPathURL to ocidDesktopPathArray's firstObject()
set ocidDesktopPath to ocidDesktopPathURL's |path|
###ブックマーク作成先
set ocidBookMarkPathURL to ocidDesktopPathURL's URLByAppendingPathComponent:(ocidBundleDisplayName) isDirectory:false
set ocidBookMarkPath to ocidBookMarkPathURL's |path|
####エイリアスを作る
set listResults to (refMe's NSURL's writeBookmarkData:(ocdiBookMarkData) toURL:(ocidBookMarkPathURL) options:(refMe's NSURLBookmarkCreationSuitableForBookmarkFile) |error|:(reference))

###########################
###アイコン付与

set boolAddIcon to appSharedWorkspace's setIcon:(ocidImageData) forFile:(ocidBookMarkPath) options:(refMe's NSExclude10_4ElementsIconCreationOption)
###選択して表示
set boolSelectFileResults to appSharedWorkspace's selectFile:(ocidBookMarkPath) inFileViewerRootedAtPath:(ocidDesktopPathURL)


|

エイリアス作成とアイコン付与


【スクリプトエディタで開く】|

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions
property refMe : a reference to current application
###初期化
set appFileManager to refMe's NSFileManager's defaultManager()
set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
###########################
###アプリケーションディレクトリ(ユーザーアプリケーション)
set ocidUserApplicationPathArray to (appFileManager's URLsForDirectory:(refMe's NSApplicationDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidApplicationDirPathURL to ocidUserApplicationPathArray's firstObject()
set ocidApplicationDirPath to ocidApplicationDirPathURL's |path|
###フォルダ作る
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidApplicationDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
###/users/XXXXX/Applications/Finderのエイリアス
set ocidBookMarkPathURL to ocidApplicationDirPathURL's URLByAppendingPathComponent:("Finder Applications") isDirectory:true
set ocidBookMarkPath to ocidBookMarkPathURL's |path|
###########################
###コアサービス
set ocidCoreServiceDirPathArray to (appFileManager's URLsForDirectory:(refMe's NSCoreServiceDirectory) inDomains:(refMe's NSSystemDomainMask))
set ocidCoreServiceDirPathURL to ocidCoreServiceDirPathArray's firstObject()
###Finder
set ocidFinderFilePathURL to ocidCoreServiceDirPathURL's URLByAppendingPathComponent:("Finder.app/Contents/Applications") isDirectory:true
###ICON
set ocidFinderIconFilePathURL to ocidCoreServiceDirPathURL's URLByAppendingPathComponent:("Finder.app/Contents/Resources/Finder.icns") isDirectory:true
###########################
###bookmark エイリアスデータ
set listBookMarkNSData to (ocidFinderFilePathURL's bookmarkDataWithOptions:(refMe's NSURLBookmarkCreationSuitableForBookmarkFile) includingResourceValuesForKeys:{refMe's NSURLCustomIconKey} relativeToURL:(missing value) |error|:(reference))
set ocdiBookMarkData to (item 1 of listBookMarkNSData)
####エイリアスを作る
set listResults to (refMe's NSURL's writeBookmarkData:(ocdiBookMarkData) toURL:(ocidBookMarkPathURL) options:(refMe's NSURLBookmarkCreationSuitableForBookmarkFile) |error|:(reference))
###########################
###アイコン付与
set ocidImageData to (refMe's NSImage's alloc()'s initWithContentsOfURL:(ocidFinderIconFilePathURL))
set boolAddIcon to appSharedWorkspace's setIcon:(ocidImageData) forFile:(ocidBookMarkPath) options:(refMe's NSExclude10_4ElementsIconCreationOption)
###選択して表示
set boolSelectFileResults to appSharedWorkspace's selectFile:(ocidBookMarkPath) inFileViewerRootedAtPath:(ocidApplicationDirPath)


|

[writeBookmarkData]Finderエイリアスを作成する

一般的には

tell application "Finder"

set aliasDesktopPath to path to desktop folder from user domain
set aliasDownlordFolder to path to downloads folder from user domain

make new alias to aliasDownlordFolder at aliasDesktopPath
end tell


#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application
property refNSString : a reference to refMe's NSString
property refNSURL : a reference to refMe's NSURL

set objFileManager to refMe's NSFileManager's defaultManager()

#####デスクトップフォルダ
set ocidDesktopDirPath to (objFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidDesktopDirPathURL to ocidDesktopDirPath's objectAtIndex:0
####デスクトップに作るエイリアスの名前
set ocidAiliasURL to ocidDesktopDirPathURL's URLByAppendingPathComponent:"Downloads"
set ocidAiliasPath to ocidAiliasURL's |path|()

####有無確認
set boolDirExists to (objFileManager's fileExistsAtPath:ocidAiliasPath isDirectory:false)
if boolDirExists is false then
###ダウンロードフォルダ
set ocidUserDownloadsPath to (objFileManager's URLsForDirectory:(refMe's NSDownloadsDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidUserDownloadsURL to ocidUserDownloadsPath's objectAtIndex:0

####エイリアスデータの取得
set listBookMarkNSData to (ocidUserDownloadsURL's bookmarkDataWithOptions:(refMe's NSURLBookmarkCreationSuitableForBookmarkFile) includingResourceValuesForKeys:{refMe's NSURLCustomIconKey} relativeToURL:(missing value) |error|:(reference))
set ocdiBookMarkData to (item 1 of listBookMarkNSData)

####エイリアスを作る
set listResults to (refNSURL's writeBookmarkData:ocdiBookMarkData toURL:ocidAiliasURL options:(refMe's NSURLBookmarkCreationSuitableForBookmarkFile) |error|:(reference))

end if

|

より以前の記事一覧

その他のカテゴリー

Accessibility Acrobat Acrobat 2020 Acrobat AddOn Acrobat Annotation Acrobat ARMDC Acrobat AV2 Acrobat BookMark Acrobat Classic Acrobat DC Acrobat Dialog Acrobat Distiller Acrobat Form Acrobat JS Acrobat Manifest Acrobat Menu Acrobat Open Acrobat Plugin Acrobat Preferences Acrobat Preflight Acrobat python Acrobat Reader Acrobat SCA Acrobat SCA Updater Acrobat Sequ Acrobat Sign Acrobat Stamps Acrobat Watermark Acrobat Windows Acrobat Windows Reader Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Mobileconfig Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin Support Admin System Information Admin Tools Admin Users Admin Volumes Adobe Adobe FDKO Adobe RemoteUpdateManager AppKit Apple AppleScript AppleScript do shell script AppleScript List AppleScript ObjC AppleScript Osax AppleScript PDF AppleScript Pictures AppleScript record AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript System Events AppleScript System Events Plist AppleScript Video Applications AppStore Archive Attributes Automator BackUp Barcode Barcode QR Barcode QR Decode Bash Basic Basic Path Bluetooth BOX Browser Calendar CD/DVD Choose Chrome CIImage CityCode CloudStorage Color com.apple.LaunchServices.OpenWith Console Contacts CotEditor CURL current application Date&Time delimiters Desktop Device Diff Disk Dock DropBox Droplet eMail Encode % Encode Decode Encode UTF8 Error EXIFData ffmpeg File Finder Firefox Folder FolderAction Fonts GIF github Guide HTML HTML Entity Icon Illustrator Image Events Image2PDF ImageOptim iPhone iWork Javascript Jedit Json Label Leading Zero List locationd LRC lsappinfo LSSharedFileList m3u8 Mail MakePDF Map Math Media Media AVAsset Media AVconvert Media AVFoundation Media AVURLAsset Media Movie Media Music Memo Messages Microsoft Microsoft Edge Microsoft Excel Mouse Music NetWork Notes NSArray NSArray Sort NSBezierPath NSBitmapImageRep NSBundle NSCFBoolean NSCharacterSet NSColor NSColorList NSData NSDecimalNumber NSDictionary NSError NSEvent NSFileAttributes NSFileManager NSFileManager enumeratorAtURL NSFont NSFontManager NSGraphicsContext NSImage NSIndex NSKeyedArchiver NSKeyedUnarchiver NSLocale NSMutableArray NSMutableDictionary NSMutableString NSNotFound NSNumber NSOpenPanel NSPasteboard NSpoint NSPredicate NSPrintOperation NSRange NSRect NSRegularExpression NSRunningApplication NSScreen NSSize NSString NSString stringByApplyingTransform NSStringCompareOptions NSTask NSTimeZone NSURL NSURL File NSURLBookmark NSURLComponents NSURLResourceKey NSURLSession NSUserDefaults NSUUID NSView NSWorkspace Numbers OAuth OneDrive PDF PDFAnnotation PDFAnnotationWidget PDFContext PDFDisplayBox PDFDocumentPermissions PDFImageRep PDFKit PDFnUP PDFOutline perl Photoshop PlistBuddy pluginkit postalcode PostScript prefPane Preview Python QuickLook QuickTime ReadMe Regular Expression Reminders ReName Repeat RTF Safari SaveFile ScreenCapture ScreenSaver SF Symbols character id SF Symbols Entity sips Skype Slack Sound Spotlight sqlite SRT StandardAdditions Swift System Settings TCC TemporaryItems Terminal Text Text CSV Text MD Text TSV TextEdit Tools Translate Trash Twitter Typography UI Unit Conversion UTType valueForKeyPath Video VisionKit Visual Studio Code VMware Fusion Wacom webarchive webp Wifi Windows XML XML EPUB XML OPML XML Plist XML RSS XML savedSearch XML SVG XML TTML XML webloc XML XMP YouTube zoom