Menu

Skip to content
AppleScriptの穴
  • Home
  • Products
  • Books
  • Docs
  • Events
  • Forum
  • About This Blog
  • License
  • 仕事依頼

AppleScriptの穴

Useful & Practical AppleScript archive. Click '★Click Here to Open This Script' Link to download each AppleScript

カテゴリー: folder

targetが重複しているFinder Windowをクローズする

Posted on 7月 22, 2021 by Takaaki Naganoya

日常的に生じている不満を解消するための、ちょっとしたかき捨てScript……になるはずだったものです。

Classic Mac OSからMac OS Xへの移行後、Finderの仕様で大きく変わった点が1つあります。「同じフォルダのウィンドウを複数枚オープンできるようになった」点です。

その結果、

といったように、同じフォルダを指し示すウィンドウが何枚もたまる現象が起きています。これは別にバグでも不具合でもありません。仕様です。

この重複Windowを掃除するためのScriptを書いてみました。

AppleScript名:ターゲットが同じWindowを取得(未遂).scpt
tell application "Finder"
  tell window 1
    set aTarg to target
  end tell
  
  
set wList to every window whose target  is equal to aTarg
end tell

★Click Here to Open This Script 

何も考えずに書き始めると、こんな(↑)感じでしょう。ただ、このScriptは動きません。フィルター参照は、たとえばファイル名や拡張子、ファイル種別(kind)あたりには使えるものの、この「target」属性に対しては効きません。

そこで、仕方なくちょっと腰をすえて書いてみました。これ(↓)を動かすと、

のように重複ウィンドウが一掃されます。これでいいはずです。

AppleScript名:targetが重複しているFinder Windowをクローズする.scpt
—
–  Created by: Takaaki Naganoya
–  Created on: 2021/07/22
—
–  Copyright © 2021 Piyomaru Software, All Rights Reserved
—

set clList to {}

tell application "Finder"
  set wList to every window
  
–> {Finder window id 1775 of application "Finder", Finder window id 1612 of application "Finder", Finder window id 1740 of application "Finder", Finder window id 1641 of application "Finder", Finder window id 1630 of application "Finder", Finder window id 1618 of application "Finder", Finder window id 1599 of application "Finder", Finder window id 1592 of application "Finder", Finder window id 1586 of application "Finder", Finder window id 1580 of application "Finder", Finder window id 1575 of application "Finder", Finder window id 1569 of application "Finder", Finder window id 1563 of application "Finder"}
  
  
set tList to (target of every window) as alias list
  
–> {alias "Macintosh HD:Users:me:Public:ドロップボックス:", alias "Macintosh HD:Users:me:Documents:AppleScript 12.0:", alias "Macintosh HD:Users:me:Public:ドロップボックス:", alias "Macintosh HD:Users:me:Public:ドロップボックス:", alias "Macintosh HD:Users:me:Public:ドロップボックス:", alias "Macintosh HD:Users:me:Public:ドロップボックス:", alias "Macintosh HD:Users:me:Public:ドロップボックス:", alias "Macintosh HD:Users:me:Public:ドロップボックス:", alias "Macintosh HD:Users:me:Public:ドロップボックス:", alias "Macintosh HD:Users:me:Public:ドロップボックス:", alias "Macintosh HD:Users:me:Public:ドロップボックス:", alias "Macintosh HD:Users:me:Public:ドロップボックス:", alias "Macintosh HD:Users:me:Public:ドロップボックス:"}
  
  
–ターゲットフォルダをユニーク化
  
set bList to removeDuplicates(tList) of me
  
–> {alias "Macintosh HD:Users:me:Documents:AppleScript 12.0:", alias "Macintosh HD:Users:me:Public:ドロップボックス:"}
  
  
–ユニーク化したターゲットでループ
  
repeat with i in bList
    set j to (contents of i) as alias
    
set firstF to true
    
    
–ウィンドウでループ
    
repeat with ii in wList
      set jj to contents of ii
      
set tmpTarg to (target of jj) as alias
      
      
if tmpTarg is equal to j then
        –最初の1つ目のWindowだけ残す
        
if firstF = true then
          set firstF to false
        else
          –2個目以降のWindowはクローズ対象リストに入れる
          
set the end of clList to jj
        end if
      end if
    end repeat
  end repeat
  
  
–クローズ対象とされるFinder Windowを順次クローズ
  
repeat with i in clList
    set j to contents of i
    
try
      ignoring application responses
        close j
      end ignoring
    end try
  end repeat
end tell

on removeDuplicates(aList)
  set newList to {}
  
repeat with i from 1 to (length of aList)
    set anItem to item 1 of aList
    
set aList to rest of aList
    
if {anItem} is not in aList then set end of newList to anItem
  end repeat
  
return newList
end removeDuplicates

★Click Here to Open This Script 

edama2さんがコメント欄に投稿してくださったプログラムを掲載しておきます。自分が「安全のためにこのぐらいの処理は必要(かも?)」と付けまくった処理を「それ別に実際のところ必要ないんじゃない?」と省略された感じです。

あとは、edama2さんのScriptだと「最前面のWindowのtargetと同じtargetを持つWindowをクローズする」という動作ですが、自分のScriptは「targetが重複しているWindowは1つを残してあとすべてをクローズする」という動作を行うので、厳密にいえば違うものです。

AppleScript名:targetが重複しているFinder Windowをクローズする v2.scpt
–  Created by: edama2
–  Created on: 2021/07/22

tell application "Finder"
  tell front window
    set aTarg to target
  end tell
  
set aList to (every Finder window) as list
  
repeat with num from 2 to count aList
    set anItem to (aList)’s item num
    
if (anItem’s target is aTarg) then close anItem
  end repeat
end tell

★Click Here to Open This Script 

(Visited 85 times, 1 visits today)
Posted in folder | Tagged 10.14savvy 10.15savvy 11.0savvy 12.0savvy Finder | 1 Comment

フォルダの存在確認

Posted on 3月 20, 2018 by Takaaki Naganoya
AppleScript名:フォルダの存在確認
— Created 2017-10-31 by Takaaki Naganoya
— 2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

set bPath to POSIX path of (choose folder)
set bExt to current application’s NSFileManager’s defaultManager()’s fileExistsAtPath:bPath isDirectory:true
–> true

★Click Here to Open This Script 

AppleScript名:フォルダの存在確認(OLD Style AS)
set aFolder to choose folder

tell application "Finder"
  set aRes to exists of aFolder
  
–> true
end tell

★Click Here to Open This Script 

(Visited 304 times, 1 visits today)
Posted in folder | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

2フォルダ間の内容比較

Posted on 3月 20, 2018 by Takaaki Naganoya
AppleScript名:2フォルダ間の内容比較
— Created 2015-12-22 by Takaaki Naganoya
— 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

set aFol to (choose folder with prompt "Select Folder A")
set bFol to (choose folder with prompt "Select Folder B")

set aPath to POSIX path of aFol
set bPath to POSIX path of bFol

set aFM to current application’s NSFileManager’s defaultManager()
set aRes to (aFM’s contentsEqualAtPath:aPath andPath:bPath) as boolean

★Click Here to Open This Script 

(Visited 15 times, 1 visits today)
Posted in file folder | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

指定フォルダからカスタムアイコンを削除する v3

Posted on 2月 8, 2018 by Takaaki Naganoya

指定のフォルダに指定されていたアイコン画像を削除するAppleScriptです。

特定のフォルダを監視して、ファイルが追加されたり削除されたりすると、指定のAppleScriptを実行する仕組み「フォルダアクション」がmacOSに標準装備されています。

フォルダアクションはフォルダアクションでいいのですが、あまり融通が効かないので、フォルダアクションを使わずにAppleScript独自でフォルダを監視することはよくあります。

そして、監視対象に指定したフォルダのアイコンを変更し、わかりやすく「監視対象である」ことをユーザーに伝えることも、よくある話です。そして、監視処理が終了したあとでフォルダのアイコンをOS標準の元のものに戻しておく必要があります。本Scriptはそういう処理に用いるものです。

AppleScript名:指定フォルダからカスタムアイコンを削除する v3
— Created 2015-10-21by Takaaki Naganoya
— 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

set aFolder to POSIX path of (choose folder with prompt "Choose Target Folder") –カスタムアイコン削除対象のフォルダ
removeCustomIcon(aFolder) of me

on removeCustomIcon(aFolder)
  set aSW to current application’s NSWorkspace’s sharedWorkspace()
  
aSW’s setIcon:(missing value) forFile:aFolder options:0 –Erase
  
  
tell application "Finder"
    update ((POSIX file aFolder) as alias) –Refresh State
  end tell
end removeCustomIcon

★Click Here to Open This Script 

(Visited 22 times, 1 visits today)
Posted in folder Icon System | Tagged 10.11savvy 10.12savvy 10.13savvy | Leave a comment

指定フォルダに指定アイコン画像をつける v3

Posted on 2月 8, 2018 by Takaaki Naganoya

指定のフォルダに指定アイコン画像をつけるAppleScriptです。

特定のフォルダを監視して、ファイルが追加されたり削除されたりすると、指定のAppleScriptを実行する仕組み「フォルダアクション」がmacOSに標準装備されています。

フォルダアクションはフォルダアクションでいいのですが、あまり融通が効かないので、フォルダアクションを使わずにAppleScript独自でフォルダを監視することはよくあります。

そして、監視対象に指定したフォルダのアイコンを変更し、わかりやすく「監視対象である」ことをユーザーに伝えることも、よくある話です。本Scriptはそういう処理に用いるものです。

もちろん、処理が終了したあとは監視対象フォルダのアイコンは元に戻しておくべきで、「指定フォルダからカスタムアイコンを削除する v3」とペアで使っています。

AppleScript名:指定フォルダに指定アイコン画像をつける v3
— Created 2015-10-21 by Takaaki Naganoya
— 2015 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

set anIconPath to POSIX path of (choose file of type {"com.apple.icns", "public.tiff"} with prompt "Choose Icon File")
set aFolder to POSIX path of (choose folder with prompt "Choose Folder")
setCustomIcon(anIconPath, aFolder) of me

on setCustomIcon(aPath, aFolder)
  set aFolderPath to current application’s NSString’s stringWithString:aFolder
  
  
set aURL to current application’s |NSURL|’s fileURLWithPath:aPath
  
set aImage to current application’s NSImage’s alloc()’s initWithContentsOfURL:aURL
  
  
set aSW to current application’s NSWorkspace’s sharedWorkspace()
  
aSW’s setIcon:(missing value) forFile:aFolder options:0 –Erase
  
tell application "Finder"
    update ((POSIX file aFolder) as alias) –Refresh State
  end tell
  
  
aSW’s setIcon:aImage forFile:aFolderPath options:0 –Write
  
tell application "Finder"
    update ((POSIX file aFolder) as alias) –Refresh State
  end tell
end setCustomIcon

★Click Here to Open This Script 

(Visited 170 times, 1 visits today)
Posted in folder Icon System | Tagged 10.11savvy 10.12savvy 10.13savvy | 1 Comment

電子書籍(PDF)をオンラインストアで販売中!

Google Search

Popular posts

  • macOS 12が正式リリースされる
  • AppleScriptからショートカット実行&ショートカット内でAppleScriptを実行
  • Xcode 13.2 〜 13.3は有害(正式版でもまだ有害)
  • Intel MacとApple Silicon Macの速度差〜画像処理
  • 2021年に書いた価値あるAppleScript
  • AS Publisher v18
  • AppleScriptによるWebブラウザ自動操縦ガイド
  • Stream Deck Softwareがバージョン5.1.2にバージョンアップ
  • CotEditorで選択範囲の行頭にある数字をリナンバーする v1
  • ドラッグ&ドロップ機能の未来?
  • macOS 12.x上のAppleScriptのトラブルまとめ
  • Shortcuts Eventsでショートカットのインストール+実行
  • Amazon EC2 M1 Macインスタンスが利用可能に
  • マウスの右クリックメニューをカスタマイズするService Station
  • macOS 12.3 beta 5、ASの障害が解消される(?)
  • 不可視プロセスで表示したNSAlertを最前面に表示
  • macOS 12.3 beta4、まだ直らないASまわりの障害
  • AppleScript Version 2.8?
  • 与えられた自然言語テキストから言語を推測して、指定の性別で、TTSキャラクタを自動選択して読み上げ
  • 集中モード(Don’t Disturb Mode)に設定する

Tags

10.11savvy (1102) 10.12savvy (1243) 10.13savvy (1391) 10.14savvy (574) 10.15savvy (414) 11.0savvy (250) 12.0savvy (122) CotEditor (54) Finder (46) iTunes (19) Keynote (88) NSAlert (60) NSArray (51) NSBezierPath (18) NSBitmapImageRep (21) NSBundle (20) NSButton (34) NSColor (51) NSDictionary (27) NSFileManager (23) NSFont (18) NSImage (42) NSJSONSerialization (21) NSMutableArray (61) NSMutableDictionary (21) NSPredicate (36) NSRunningApplication (56) NSScreen (30) NSScrollView (22) NSString (117) NSURL (97) NSURLRequest (23) NSUTF8StringEncoding (30) NSUUID (18) NSView (33) NSWorkspace (20) Numbers (53) Pages (33) Safari (38) Script Editor (17) WKUserContentController (21) WKUserScript (20) WKUserScriptInjectionTimeAtDocumentEnd (18) WKWebView (22) WKWebViewConfiguration (22)

カテゴリー

  • 2D Bin Packing
  • AirDrop
  • AirPlay
  • Animation
  • AppleScript Application on Xcode
  • Bluetooth
  • Books
  • boolean
  • bounds
  • Bug
  • Calendar
  • call by reference
  • Clipboard
  • Code Sign
  • Color
  • Custom Class
  • dialog
  • drive
  • exif
  • file
  • File path
  • filter
  • folder
  • Font
  • Font
  • GAME
  • geolocation
  • GUI
  • GUI Scripting
  • History
  • How To
  • Icon
  • Image
  • Input Method
  • Internet
  • iOS App
  • JavaScript
  • JSON
  • JXA
  • Keychain
  • Keychain
  • Language
  • list
  • Locale
  • Machine Learning
  • Markdown
  • Menu
  • Metadata
  • MIDI
  • MIME
  • Natural Language Processing
  • Network
  • news
  • Noification
  • Notarization
  • Number
  • Object control
  • OCR
  • OSA
  • PDF
  • Peripheral
  • PRODUCTS
  • QR Code
  • Raw AppleEvent Code
  • Record
  • recursive call
  • regexp
  • Release
  • Remote Control
  • Require Control-Command-R to run
  • REST API
  • Review
  • RTF
  • Sandbox
  • Screen Saver
  • Script Libraries
  • sdef
  • search
  • Security
  • selection
  • shell script
  • Shortcuts Workflow
  • Sort
  • Sound
  • Spellchecker
  • Spotlight
  • SVG
  • System
  • Tag
  • Telephony
  • Text
  • Text to Speech
  • timezone
  • Tools
  • Update
  • URL
  • UTI
  • Web Contents Control
  • WiFi
  • XML
  • XML-RPC
  • イベント(Event)
  • 未分類

アーカイブ

  • 2022年7月
  • 2022年6月
  • 2022年5月
  • 2022年4月
  • 2022年3月
  • 2022年2月
  • 2022年1月
  • 2021年12月
  • 2021年11月
  • 2021年10月
  • 2021年9月
  • 2021年8月
  • 2021年7月
  • 2021年6月
  • 2021年5月
  • 2021年4月
  • 2021年3月
  • 2021年2月
  • 2021年1月
  • 2020年12月
  • 2020年11月
  • 2020年10月
  • 2020年9月
  • 2020年8月
  • 2020年7月
  • 2020年6月
  • 2020年5月
  • 2020年4月
  • 2020年3月
  • 2020年2月
  • 2020年1月
  • 2019年12月
  • 2019年11月
  • 2019年10月
  • 2019年9月
  • 2019年8月
  • 2019年7月
  • 2019年6月
  • 2019年5月
  • 2019年4月
  • 2019年3月
  • 2019年2月
  • 2019年1月
  • 2018年12月
  • 2018年11月
  • 2018年10月
  • 2018年9月
  • 2018年8月
  • 2018年7月
  • 2018年6月
  • 2018年5月
  • 2018年4月
  • 2018年3月
  • 2018年2月

https://piyomarusoft.booth.pm/items/301502

メタ情報

  • 登録
  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org

Forum Posts

  • 人気のトピック
  • 返信がないトピック

メタ情報

  • 登録
  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org
Proudly powered by WordPress
Theme: Flint by Star Verte LLC